【问题标题】:Problems executing Cisco Python script执行 Cisco Python 脚本的问题
【发布时间】:2015-10-23 18:28:46
【问题描述】:

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

有什么想法吗?

【问题讨论】:

  • 查看telnetlib地址需要什么样的格式。通常端口是一个单独的参数。
  • 你有什么开关?我尝试使用HOST="10.1.1.1" 在 Cisco 2600 上运行它。您可能必须按照上面的建议删除端口。
  • 根据上述 cmets,docs.python.org/2/library/telnetlib.html 声明主机和端口绝对是分开的。 HOST = "10.252.128.81",然后像 tn = telnetlib.Telnet(HOST, '3132') 一样打电话,看看是否有帮助。

标签: python python-3.x cisco-ios


【解决方案1】:

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

result = Terminal.ExecCommand("sh run","#")
commandResult = result[0]
timedOut = result[1]
if not timedOut:
  ActionResult = commanResult
  ScriptSuccess = True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多