将不成功的连接尝试输出追加到.txt文件

时间:2019-04-26 16:24:54

标签: networking network-programming cisco-ios

我在保存将配置应用于多个设备的脚本的输出时遇到问题,并且让我知道与设备的连接是否超时。脚本成功登录到设备,运行命令并显示输出。它将超时的设备保存到.txt文件,但是,如果多个设备遇到超时,则会删除带有超时设备的行,并且仅显示最后一个超时的设备的名称。有人可以告诉我我应该添加到代码中的内容,以便脚本无法连接的所有设备的名称吗?

Timeouts = open(“连接超时。txt”,“ a”)

try: 
    net_connect = ConnectHandler(**ios_device)  
except (AuthenticationException):
    print ('Authentication Failure: ' + ip_address_of_device)
    continue 
except (NetMikoTimeoutException):
    print ('Timeout to device: ' + ip_address_of_device)
    Timeouts.write(ip_address_of_device)
    continue
except (EOFError):
    print ('End of file whilte attempting device: ' + ip_address_of_device)
    continue
except (SSHException):
    print ('SSH might not be enabled: ' + ip_address_of_device)
    continue 
except Exception as unknown_error:
    print (str(unknown_error))
    continue

#with open ("Connection time out.txt") as f: 
    #for line in f: 
        #print (line), 


output=net_connect.send_config_set(commands_list)
print(output)

1 个答案:

答案 0 :(得分:0)

f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()

#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())