平时工作用的小脚本,放上来
import telnetlib def get_mac(): coreip='三层交换机IP' # try: tel = telnetlib.Telnet(coreip) tel.read_until('Username:') tel.write('三层交换机账号'+'\n') tel.read_until('Password:') tel.write('三层交换机密码'+'\n') tel.read_until('>') tel.write('ping ' + s_ip +'\n') # data = tel.read_until('>') tel.read_until('>') tel.write('dis arp | in ' + s_ip +'\n') data = tel.read_until('>') data = data.split() print data[16] mac = data[16] return mac # except: print 'bad' def get_port(): # try: tel = telnetlib.Telnet(ip) tel.read_until('Username:') tel.write('账号'+'\n') tel.read_until('Password:') tel.write('密码'+'\n') tel.read_until('>') tel.write('dis mac-add ' + mac +'\n') raw_data = tel.read_until('>') print '*************** connect to ========>>',ip if 'XGE' not in raw_data : # print raw_data data = raw_data.split() # print data if 'GE' in data[12] : print raw_data # except:print 'can not connect to %s\n' %ip while True: ipfile = open('swiplist.txt') ipall = [ ip.strip('\n') for ip in ipfile.readlines() ] s_ip = raw_input('input ip add ,such as => 192.168.0.1 \n >') mac = get_mac() for ip in ipall: get_port() words = raw_input("input 'yes' to run again, input any key to exit:") if words !='yes':break