运行以下我从 stackoverflow 下来的脚本,登录到连接到控制台服务器的 Cisco 交换机,然后发出“sh run”命令。
#!usr/bin/python
import getpass
import telnetlib
HOST = "10.252.128.81:3132"
user = input("Enter your username: ")
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 run\n")
tn.write("exit\n")
print(tn.read_all())
Python 新手,所以当我运行脚本时,我得到以下输出。
Enter your username: user1
Warning: Password input may be echoed.
Password: cisco123
Traceback (most recent call last):
File "D:\Users\ted\workspace\Test\cisco.py", line 10, in <module>
tn = telnetlib.Telnet(HOST)
File "C:\Python34\lib\telnetlib.py", line 221, in __init__
self.open(host, port, timeout)
File "C:\Python34\lib\telnetlib.py", line 237, in open
self.sock = socket.create_connection((host, port), timeout)
File "C:\Python34\lib\socket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Python34\lib\socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed
有什么想法吗?