查找IP連接在交換機的哪個端口

平時工作用的小腳本,放上來


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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章