使用python telnet cisco开关

时间:2013-10-30 00:56:19

标签: python cisco

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

运行脚本后它会挂起。我该如何解决这个问题?

5 个答案:

答案 0 :(得分:2)

你应该看看Trigger:https://trigger.readthedocs.org/en/latest/

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

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/

答案 1 :(得分: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')

答案 2 :(得分:0)

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

答案 3 :(得分:0)

  

用于cisco路由器和交换机的Cisco Python Telnet脚本   用于telneting和配置第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脚本

答案 4 :(得分: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")
相关问题