Python编程:独立于设备平台的网络设备自动备份(思科,朱尼珀,Fortigate,检查点)

2024-04-27 22:24:10 发布

您现在位置:Python中文网/ 问答频道 /正文

import telnetlib
import sys
import os
import subprocess
import time
import datetime

now = datetime.datetime.now()
username = "*****"
password = "******"
buff = ''
resp = ''
with open('Fetch.txt') as f:
    for line in f:
        line = line.strip()
        with open(os.devnull, "wb") as limbo:
            ip = line
            result = subprocess.Popen(["ping", "-n", "2", "-w", "1000", ip],
                                      stdout=limbo,
                                      stderr=limbo).wait()
            if result:
                print ip, "Link Down - Site unreachable"
                f = open('DownSites.txt', 'a+')
                f.write( line + '\n' )
                f.close()
            else:
                try:
                    tn = telnetlib.Telnet(line)
                    tn.read_until("Username:")
                    tn.write(username+"\n")
                    tn.read_until("Password:")
                    tn.write(password+"\n")
                    tn.write("terminal length 0"+"\n")
                    tn.write("sh ver"+"\n")
                    tn.write("sh processes cpu history"+"\n")
                    tn.write("sh run"+"\n")
                    tn.write("exit"+"\n")
                    output=tn.read_all()
                    print ip, "Reachable ~ Command Executed"
                    tn.close()
                    fp=open(line + '.txt',"w")
                    fp.write(output)
                    fp.close()
                except Exception as e:
                    print ip, "ERROR: ", e

这正是im试图使用pycharm community edition编译器运行的程序,以便备份独立于制造商编译过程中弹出的错误是生成的字节码应该是一个对象而不是STR。如果有比这更好的代码,请帮助我请告诉我同样的情况。在


Tags: importiptxtclosereaddatetimeassh