2

我正在通过 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()

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

4

5 回答 5

2

你应该看看触发器: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/

于 2013-10-30T01:16:08.993 回答
1

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

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')

等等。

于 2013-10-30T01:03:44.170 回答
0

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

于 2013-10-30T01:03:42.730 回答
0

用于 cisco 路由器和交换机的 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()

代码链接: 在此处下载脚本

脚步:

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

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

  3. 运行python脚本

于 2018-02-13T17:12:33.173 回答
0

我写了一个类似的代码并得到了类似的错误。然后我让代码发声以知道我在哪里犯了错误。我得出的结论是:“一直使用 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")
于 2019-05-03T16:52:37.723 回答