我正在尝试从文本文件中读取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")
答案 0 :(得分:1)
也许您想这样做?
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()