当前位置: 首页 > Conf, Python > 正文

pmzyy : dcn.py

[root@netman python]# cat cisco.py 
#!/usr/bin/env python
#This Script expects you are providing a level 15 user priveledge
#Uses FTP & Telnet and is insecure
#Will work if run through a linux terminal, not windows
#needs ftp server setup and username password added in the cisco device
#and is only a sample reference to get started.
import pexpect
import sys
import time
import datetime
 
class CiscoSwitch():
     
    def __init__(self, host, username, password):
        self.username = username
        self.host = host
        self.password = password
 
    def Login(self):
        self.child = pexpect.spawn('telnet '+self.host)
        self.child.expect('login:')
        self.child.sendline(self.username)
        self.child.expect('Password:')
        self.child.sendline(self.password)
        self.child.expect('#')
        return (self.child, self.child.before)
 
    def RunShowCmd(self,cmd):
        self.child.sendline(cmd)
        self.child.expect('#')
        return (self.child, self.child.before)
     
    def FtpBackupCmd(self,ftpip):
        self.child.sendline('copy running-config tftp://')
        self.child.expect(']:')
        self.child.sendline(ftpip)
        self.child.expect('>')
        DATE = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
        self.child.sendline(self.host+'--'+DATE+'.conf')
        self.child.expect(']:')
        self.child.sendline('Y')
        time.sleep(2)
        self.child.expect('#')
        return (self.child, self.child.before)
 
if __name__ == '__main__':
        print 'This program is being run by itself'
        Switch = CiscoSwitch('100.1.2.254','zyxxk','zyxxk')
        (obj,stdout) = Switch.Login()
        print stdout
        #(obj,stdout) = Switch.RunShowCmd('show arp | inc 192.0.0.123')
        (obj,stdout) = Switch.RunShowCmd('show arp | inc 100.1.2.1')
        time.sleep(2)
        print stdout
        (obj,stdout) = Switch.FtpBackupCmd('192.0.1.63')
        print stdout


[root@netman python]# python dcn.py 
This program is being run by itself
*****
NeiKeLou-Hexin
show arp | inc 100.1.2.1
100.1.2.1        00-03-0f-62-bf-6f  Vlan700       Port-Channel5         Dynamic   331                 700
100.1.2.10       00-60-16-78-13-cc  Vlan700       Port-Channel4         Dynamic   1195                700
100.1.2.11       00-60-16-78-1c-0c  Vlan700       Port-Channel4         Dynamic   1195                700
100.1.2.100      00-50-56-a1-10-de  Vlan700       Port-Channel5         Dynamic   1196                700
100.1.2.110      00-e0-ec-2c-17-e0  Vlan700       Port-Channel4         Dynamic   1198                700
100.1.2.111      6c-ae-8b-6f-33-15  Vlan700       Port-Channel4         Dynamic   1111                700
100.1.2.113      6c-ae-8b-6f-32-7d  Vlan700       Port-Channel4         Dynamic   241                 700
100.1.2.115      6c-ae-8b-6f-33-81  Vlan700       Port-Channel4         Dynamic   1111                700
100.1.2.117      6c-ae-8b-6f-33-21  Vlan700       Port-Channel4         Dynamic   1111                700
100.1.2.122      00-e0-ec-2c-17-e0  Vlan700       Port-Channel4         Dynamic   811                 700
100.1.2.123      00-e0-ec-2c-17-e0  Vlan700       Port-Channel4         Dynamic   480                 700
100.1.2.130      00-e0-ec-2c-18-50  Vlan700       Port-Channel4         Dynamic   1168                700
100.1.2.131      6c-ae-8b-6f-33-69  Vlan700       Port-Channel4         Dynamic   91                  700
100.1.2.133      6c-ae-8b-6f-34-05  Vlan700       Port-Channel4         Dynamic   1111                700
100.1.2.135      6c-ae-8b-6f-31-b1  Vlan700       Port-Channel4         Dynamic   1111                700
100.1.2.137      6c-ae-8b-6f-30-ed  Vlan700       Port-Channel4         Dynamic   1111                700
100.1.2.142      00-e0-ec-2c-18-50  Vlan700       Port-Channel4         Dynamic   841                 700
100.1.2.143      00-e0-ec-2c-18-50  Vlan700       Port-Channel4         Dynamic   361                 700
100.1.2.161      00-50-56-a1-78-0f  Vlan700       Port-Channel5         Dynamic   661                 700
100.1.2.162      00-50-56-a1-78-0f  Vlan700       Port-Channel5         Dynamic   751                 700
100.1.2.166      00-50-56-a1-78-67  Vlan700       Port-Channel5         Dynamic   1                   700
NeiKeLou-Hexin

Begin to send file, please wait...

[root@netman python]# 

本文固定链接: http://t.yjsec.com/index.php/2018/02/08/363/ | 下一站

该日志由 admin 于2018年02月08日发表在 Conf, Python 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: pmzyy : dcn.py | 下一站

pmzyy : dcn.py:等您坐沙发呢!

发表评论

快捷键:Ctrl+Enter