我正在尝试从文本文件中读取 2 个 IP 地址并连接这些设备并在这些设备上运行“conf t”命令。当我尝试通过以下编码来完成这项工作时,python 仅读取文本文件中的最后一行,而不读取第一行。我应该怎么办?谢谢。
import paramiko
username = "xxxx"
password = "yyyy"
f = open("C:\\Users\0\Desktop\\deneme.txt")
for line in f:
ip_address = line.strip()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address, username=username, password=password)
f.close()
print ("Successfull", ip_address)
remote_connection = ssh_client.invoke_shell()
remote_connection.send("conf t\n")
也许,你想这样做?
import paramiko
username = "xxxx"
password = "yyyy"
f = open("C:\\Users\0\Desktop\\deneme.txt")
for line in f:
ip_address = line.strip()
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address, username=username, password=password)
print ("Successfull", ip_address)
remote_connection = ssh_client.invoke_shell()
remote_connection.send("conf t\n")
f.close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)