【问题标题】:Telnet cisco switch using python使用python Telnet cisco 交换机
【发布时间】:2019-05-04 00:52:37
【问题描述】:

我正在通过 python 脚本远程登录到 cisco 交换机。代码如下:

#!/usr/bin/python
import getpass
import sys
import telnetlib

HOST = "10.203.4.1"
user = raw_input("Enter your remote account: ")
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("vt100\n")
tn.write("ls\n")
tn.write("exit\n")
print tn.read_all()

它只是在运行脚本后挂断。我该如何解决这个问题?

【问题讨论】:

  • 也许 Cisco 写 Username:Login: 但您的代码正在等待 login:。使用print 在屏幕上为您写下更多信息 - 例如“现在我将等待'登录:'”等。

标签: python cisco


【解决方案1】:

你应该看看触发器:https://trigger.readthedocs.org/en/latest/

这是一个与网络设备交互的自动化工具包,例如思科路由器/交换机:

from trigger.cmds import Commando

class ShowClock(Commando):
    """Execute 'show clock' on a list of Cisco devices."""
    vendors = ['cisco']
    commands = ['show clock']

if __name__ == '__main__':
    device_list = ['foo1-abc.net.aol.com', 'foo2-xyz.net.aol.com']
    showclock = ShowClock(devices=device_list)
    showclock.run() # Commando exposes this to start the event loop

    print '\nResults:'
    print showclock.results

查看文档了解更多信息:https://trigger.readthedocs.org/en/latest/

【讨论】:

    【解决方案2】:

    这是一个更简单的解决方案:

    import pexpect
    import getpass
    
    HOST = "10.203.4.1"
    user = raw_input("Enter your remote account: ")
    password = getpass.getpass()
    
    child = pexpect.spawn ('telnet '+HOST)
    child.expect ('Username: ')
    child.sendline (user)
    child.expect ('Password: ')
    child.sendline (password)
    # If the hostname of the router is set to "deep"
    # then the prompt now would be "deep>"
    routerHostname = "deep" #example - can be different
    child.expect (routerHostname+'>')
    child.sendline ('enable')
    

    等等

    【讨论】:

    • 请您更详细地解释最后 3 行。或者如果主机名是别的东西怎么办。请你详细说明。谢谢
    • 所以当您通过 telnet 登录路由器时,您必须考虑到这一点。确保您可以从命令行执行telnet 10.203.4.1,然后您必须获得Username: 提示,然后是Password: 提示等。如果您成功登录,您将能够看到您收到的下一个提示通过远程登录。在我上面的程序中,我手动将主机名设置为"deep" - 你可以使用任何你喜欢的东西。下一个child.expect 基本上希望看到一个带有主机名的提示。
    • 有什么方法可以通过pexpect从交换机获取主机名吗?谢谢。
    【解决方案3】:

    首先请考虑使用 telnet 以外的其他方式。 SSH 是一个很好的替代品。 其次,要使这个 pythonic 使用一个名为 pexpect 的库来做这件事。最后一行将使用命令 .interact() 再次获得控制权。

    【讨论】:

    • 思科交换机上的 ssh 端口已关闭。
    • 某些设备由于“遗留”原因不支持 telnet。有时,这些遗留原因是企业文化所固有的。啊……技术。
    【解决方案4】:

    用于思科路由器和交换机的 Cisco Python Telnet 脚本 用于远程登录和配置第 3 层设备的最佳且简单的脚本。

    import getpass
    import sys
    import telnetlib
    
    HOST = "YOUR ROUTER IP ADDRESS"
    user = raw_input("Enter your telnet username: ")
    password = getpass.getpass()
    
    tn = telnetlib.Telnet(HOST)
    
    tn.read_until("Username: ")
    tn.write(user + "\n")
    if password:
    tn.read_until("Password: ")
    tn.write(password + "\n")
    
    
     tn.write("exit\n")
    
      print tn.read_all()
    

    代码链接: Download the script here

    步骤:

    1. 安装了python的终端设备并将终端设备连接到路由器

    2. 配置telnet和用户名密码数据库

    3. 运行python脚本

    【讨论】:

      【解决方案5】:

      我写了一个类似的代码,得到了类似的错误。然后我让代码发声以知道我在哪里犯了错误。我得出的结论是: “一直使用 read_all() 函数并不是一个好主意。它无限读取并带来类似挂起模式的印象。尝试在读取过程中将其替换为设备提示和计时器。并尝试打印它以查看代码是否捕获了所需的输出"

      import telnetlib
      import os
      import sys
      
      host = raw_input("Enter the VG IP : ")
      user = "cisco"
      password = "cisco"
      #cmd = raw_input("Enter the command you want to feed : ")
      cmd1 = "term len 0"
      cmd = "show clock"
      pingable = False
      
      response = os.system("ping -c 2 " + host)
      if response == 0:
          pingable = True
          print(host, "is Pingable", pingable)
      else:
          print(host, "is un-Pingable", pingable)
      
      if(pingable):
          tn = telnetlib.Telnet(host)
          banner = tn.read_until("Username:", 5)
          tn.write(user + "\n")
          print(banner)
          tn.read_until("Password:", 5)
          tn.write(password1 + "\n")
          prompt = tn.read_until("#")
          print("I am logged in\n\n")
          print(prompt)
          tn.write(cmd1 + b"\n")
          output1 = tn.read_until("#",5)
          print("my first cmd output is :", output1, "\n")
          tn.write(cmd + "\n")
          output1 = tn.read_until("#",5)
          print("My 2nd cmd is feeded here", output1)
          tn.write("show version\n")
          output1 = tn.read_until("more-- ",5)
          print("version info : ", output1)
          tn.write("exit\n")
      
      else:
          print(host, "is unpingable")
      

      【讨论】:

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