1. 程式人生 > >Python與Cisco 的事兒

Python與Cisco 的事兒

elf 分享圖片 program watermark size python lac com user

今天花了一個小時研究了Python的另外一個操作網絡設備的插件Netmiko,這玩意還是滿好玩的,只是我還是整不明白如何多線程,還是得努力努力。

#!/usr/bin/python

from netmiko import ConnectHandler
import time

class CiscoNetwork:
        def __init__(self):
                 pass
        def CiscoDevice(self,iplist):
                 device={'device_type':'cisco_ios',
                         'ip':iplist,
                         'username':'admin',
                         'password': 'Password.123'
                         }
                 print "connect to network device... %s" %(iplist)
                 self.connect = ConnectHandler(**device)
                 self.connect.enable()
                 time.sleep(0.5)

        def gethostname(self):
                #用來獲取設備的hostname
                self.hostname = self.connect.find_prompt()
                self.hostname = self.hostname.replace("#","")
                print self.hostname

        def show(self,cmd):
                 #執行命令
                 self.output = self.connect.send_command(cmd)
                 print self.output

        def close(self):
                self.connect.close()

if __name__ == '__main__':
        print "[+] This Program is beging done......."
        for iplist in open("/opt/other/ip.txt"):
                switch = CiscoNetwork()
                switch.CiscoDevice(iplist)
                switch.gethostname()
                switch.show('show ip int brief')


技術分享圖片

Python與Cisco 的事兒