【问题标题】:Python, access to cisco switch via telnetPython,通过telnet访问cisco交换机
【发布时间】:2015-11-21 16:17:04
【问题描述】:

我安装 python 2.7.10 并尝试以下代码:

import getpass
import sys
import telnetlib

HOST = '172.17.0.42'
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST,23,5)

tn.read_until("Username: ", 5)
tn.write(user + "\n")
if password:
    tn.read_until("Password: ", 5)
    tn.write(password + "\n")

tn.write("en\n")
tn.write("sh ver\n")
tn.write("exit\n")

print tn.read_all()

我有这个错误:

D:>test.py
输入您的远程帐户:vfomin
密码:
回溯(最近一次通话最后一次):
文件“D:\test.py”,第 21 行,在
打印 tn.read_all()
文件“C:\Python27\lib\telnetlib.py”,第 385 行,在 read_all 中 self.fill_rawq()
文件“C:\Python27\lib\telnetlib.py”,第 576 行,在 fill_rawq buf = self.sock.recv(50)
socket.timeout: 超时

如何使用 python 连接到 172.17.0.42?

【问题讨论】:

    标签: python telnet


    【解决方案1】:

    试试这个库(适用于 python 2.x):

    pip install git+https://github.com/sergeyzelyukin/cisco-telnet.git
    
    import ciscotelnet
    with ciscotelnet.CiscoTelnet(host, verbose = False) as cisco:
      #if cisco.login(final_mode=CiscoTelnet.MODE_ENABLE, user="john", user_pass="12345678", enable_pass="cisco"):
      if cisco.login(final_mode=CiscoTelnet.MODE_ENABLE, line_pass="abcdef", enable_pass="cisco"):
        print cisco.cmd("sh int status | inc Fa0/1")
        print cisco.conf(["interface fast0/1", "descr blank", "load-interval 300"])
        print cisco.wr()
    

    【讨论】:

      【解决方案2】:

      看来您使用的是 Python3,所以您需要 input 而不是 raw_input

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-23
        • 1970-01-01
        • 2012-05-25
        • 2013-11-07
        • 1970-01-01
        • 2011-09-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多