本實驗使用了telnetlib 庫
1、拓撲
2、環境
Python 二、Cisco-Router、Kali-Linux
3、配置
!!腳本和IP地址文件都在同一個目錄下
3.一、IP 地址網絡
3.二、Python 備份腳本ide
#backup.py import time from telnetlib import Telnet def tel(addr,user,pwd,secret): tn = Telnet(addr) tn.write(user+'\n') tn.write(pwd+'\n') tn.write('enable\n') tn.write(secret+'\n') tn.write('terminal length 0\n')#將show run的內容一次性所有顯示完 time.sleep(1) tn.write('show run\n') time.sleep(1) rsp = tn.expect([],timeout=1)[2] return rsp if __name__ == "__main__": fp = open('./ip.txt','r') for ip in fp: print("backing up "+ip.strip()) conf = tel(ip.strip(),'cisco','cisco','cisco') #第一個cisco 是帳戶,第二個Cisco是密碼,第三個Cisco是enable密碼 print(ip.strip()+' was finished!') print(conf)#這裏是用於查看函數返回的內容,能夠刪除 fw = open(ip.strip(),'w')#每臺主機的配置以IP地址爲文件名,建議先使用OS模塊建立一個目錄,而後將全部配置放到目錄下 fw.write(conf) fw.close() print('done!') fp.close()
效果以下:
函數