将配置复制到文件中的脚本问题?

时间:2018-12-22 00:43:28

标签: python python-3.x

import getpass
import telnetlib
import time
import socket
import sys

#Ask for the username and password
user = input("Enter your telnet username: ")
password = getpass.getpass()

#Open a file called myswitches
f = open("ipadd.txt")

#Telnet to network devices
for line in f:
    print ("Getting running config from devices " + line)
    HOST = line.strip()
    tn = telnetlib.Telnet(HOST)
    print("host:",HOST)
    tn.read_until(b"Username:")
    tn.write(user.encode("ascii")+ b"\n")

    if password:
        tn.read_until(b"Password:")
        tn.write(password.encode("ascii")+b"\n") 

        tn.read_until(b"#")
        tn.write(b"conf t"+b"\n")
        tn.write(b"hostname test5"+b"\n")
        tn.write(b"exit"+b"\n")
        tn.write(b"terminal length 0"+b"\n")
        tn.write(b"show run"+b"\n")
        time.sleep(2)
        tn.write(b"exit"+b"\n")
#Save method
##        readoutput = tn.read_all()
        readoutput = tn.read_all().decode('ascii')
        saveoutput = open("device" + HOST, "w")
        saveoutput.write(str(readoutput))
        print (tn.read_all())
        saveoutput.close
##        tn.close()

嗨,我有这段代码可以从“ ipadd.txt” 访问设备并更改主机名并复制/导出配置,此txt文件具有2个IP地址(例如1.1.1.1) &1.1.1.2),现在运行此脚本之后...我能够完成第一个ip 1.1.1.1(更改主机名和备份),但是对于1.1.1.2,脚本只需更改主机名并导出文件,但是没有内容。

所以我想问一下为什么脚本创建文件但不从该IP写入/写入任何日期?

谢谢

0 个答案:

没有答案