-1

运行以下我从 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

有什么想法吗?

4

1 回答 1

0

没有严格回答您的问题,但根据您的目标可能会有所帮助。我正在使用 PrettyGoodTerminal 为 Cisco 路由器编写脚本,因为它为我管理连接,而 Python 部分更多的是关于发送命令和处理结果。对于简单的“sh run”,您也不需要 Pytonh,但看起来像这样:

result = Terminal.ExecCommand("sh run","#")
commandResult = result[0]
timedOut = result[1]
if not timedOut:
  ActionResult = commanResult
  ScriptSuccess = True
于 2016-04-06T10:03:13.637 回答