我在linux机器上运行python(版本2.6.2)并且需要能够telnet到一长串设备(大约300)发送命令“sho ser”并返回该命令的结果输出到列表中的文件和每个顺序设备然后将其输出添加到新行。所有设备都列在文件的单独行中
我发现一些代码让我从另一篇文章开始,但我不知道从哪里开始。我可以打开我称为“主机列表”的列表,但我不知道如何告诉python telnet到一个设备,运行命令,退出,然后转到下一个。任何帮助将不胜感激
import getpass
import sys
import telnetlib
f = open('hostlist', 'r')
HOST = "%s" % f
user = raw_input("Enter your login name: ")
password = getpass.getpass()
tn = telnetlib.Telnet(HOST)
tn.read_until("login: ")
tn.write(user + "\n")
if password:
tn.read_until("Password: ")
tn.write(password + "\n")
tn.write("show ver\n")
tn.write("exit\n")
print tn.read_all()
收到此错误
Enter your login name: K3grb6hj9ld93a2
Password:
Traceback (most recent call last):
File "test.py", line 11, in <module>
tn = telnetlib.Telnet(HOST)
File "/usr/lib/python2.6/telnetlib.py", line 209, in __init__
self.open(host, port, timeout)
File "/usr/lib/python2.6/telnetlib.py", line 225, in open
self.sock = socket.create_connection((host, port), timeout)
File "/usr/lib/python2.6/socket.py", line 498, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known