Ок. Понял.
Спасибо за объяснение.
#!/usr/bin/env python
import sys
import yapsnmp
import curses
host = ''
vers = 1
comm = 'public'
color_mode = 0
len_argv = len(sys.argv)
if len_argv < 1:
sys.exit()
elif len_argv == 1:
print "You should use it with the keys." + \
"\n\t -h <ip|name> : Destination host name or ip" + \
"\n\t -v <1|2> : snmp version" + \
"\n\t -c <string> : <community>" + \
"\n\t -? : This help"
sys.exit()
elif len_argv > 1:
for i in range(1, len_argv):
if i%2 == 1:
if sys.argv[i] == '-h':
try:
host = sys.argv[i+1]
except IndexError:
print "Error. You should enter the name of the host."
sys.exit()
elif sys.argv[i] == '-v':
try:
vers = int(sys.argv[i+1])
except IndexError:
print "Error. You should enter version of snmp."
sys.exit()
elif sys.argv[i] == '-c':
try:
comm = sys.argv[i+1]
except IndexError:
print "Error. You should enter community string."
sys.exit()
elif sys.argv[i] == '-m':
color_mode = 1
else:
print "Unknown key:", sys.argv[i]
sys.exit()
try:
dev_ifOperStatus = yapsnmp.Session(host, version=vers, community=comm).walk('ifOperStatus')
dev_ifAdminStatus = yapsnmp.Session(host, version=vers, community=comm).walk('ifAdminStatus')
dev_ifSpeed = yapsnmp.Session(host, version=vers, community=comm).walk('ifSpeed')
stdscr = curses.initscr()
stdscr.clear()
curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_BLACK)
pos = 1
len_dev_ifOperStatus = len(dev_ifOperStatus)
len_dev_ifAdminStatus = len(dev_ifAdminStatus)
len_dev_ifSpeed = len(dev_ifSpeed)
if len_dev_ifOperStatus == len_dev_ifAdminStatus and len_dev_ifOperStatus == len_dev_ifSpeed:
len_all = len_dev_ifOperStatus
for i in range(1, len_all-3):
ifOperStatus = dev_ifOperStatus[i]
ifAdminStatus = dev_ifAdminStatus[i]
ifSpeed = dev_ifSpeed[i]
if ifAdminStatus[1] == 'up' and ifOperStatus[1] == 'up' and ifSpeed[1] == '100000000':
#green
stdscr.addstr(11,pos, str(i), curses.color_pair(1) + curses.A_BOLD)
stdscr.refresh()
elif ifAdminStatus[1] == 'up' and ifOperStatus[1] == 'down':
# light black
stdscr.addstr(11,pos, str(i), curses.color_pair(4) + curses.A_BOLD)
stdscr.refresh()
elif ifAdminStatus[1] == 'down' and ifOperStatus[1] == 'down':
#white
stdscr.addstr(11,pos, str(i), curses.color_pair(3))
stdscr.refresh()
elif ifAdminStatus[1] == 'down' and ifOperStatus[1] == 'up':
#white
stdscr.addstr(11,pos, str(i), curses.color_pair(3))
stdscr.refresh()
elif ifAdminStatus[1] == 'up' and ifSpeed[1] == '10000000':
#light yellow
stdscr.addstr(11,pos, str(i), curses.color_pair(2) + curses.A_BOLD)
stdscr.refresh()
else:
print 'Unknown configuration of port'
if i < 10:
pos += 2
if i >= 10:
pos += 3
else:
print 'Error!'
stdscr.addstr(13,1, "Legend:", curses.color_pair(3) + curses.A_BOLD + curses.A_UNDERLINE)
stdscr.addstr(15,3, "* ", curses.color_pair(1) + curses.A_BOLD)
stdscr.addstr(16,3, "* ", curses.color_pair(2) + curses.A_BOLD)
stdscr.addstr(17,3, "* ", curses.color_pair(3))
stdscr.addstr(18,3, "* ", curses.color_pair(4) + curses.A_BOLD)
stdscr.addstr(15,5, " - Port is Up, and Admin status 'enabled', and speed on port 100 Mbits per second ", curses.color_pair(3))
stdscr.addstr(16,5, " - Port is Up, and Admin status 'enabled', and speed on port 10 Mbits per second", curses.color_pair(3))
stdscr.addstr(17,5, " - Admin status 'disable'", curses.color_pair(3))
stdscr.addstr(18,5, " - port is down", curses.color_pair(3))
stdscr.refresh()
curses.endwin()
except yapsnmp.GetError:
print "No responce from", host
except yapsnmp.MIBParseError:
print "MIB error"
except KeyboardInterrupt:
print "User interrupt"
Андрей писал(а):В итоге код получился таким
root писал(а):ну не плохо было бы показать и скрин вывода этого скрипта, чтобы нагляднее было
Вернуться в Программинг и сети
Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 9