使用python的Telnet cisco交换机
我通过python脚本远程连接到cisco交换机。守则如下:使用python的Telnet cisco交换机,python,cisco,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.wr
#!/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()
它只是在运行脚本后挂起。我如何解决这个问题? 首先请考虑使用telnet以外的东西。SSH是一个很好的替代品。
其次,要使这个pythonic使用一个名为pexpect的库来完成这项工作。最后一行将使用命令.interact()再次获得控制权。这里有一个更简单的解决方案:
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')
等等。您应该查看触发器: 它是一个与网络设备(如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
查看文档以了解更多信息:
Cisco路由器和交换机的Cisco Python Telnet脚本
用于远程联网和配置第3层设备的最佳简单脚本
代码链接:
步骤:
我写了一个类似的代码,得到了类似的错误。然后我将代码发声,以了解我在哪里出错。我的结论是: “始终使用read_all()函数不是一个好主意。它可以无限读取,给人留下挂起模式的印象。在读取过程中,尝试用设备提示和计时器替换它。然后尝试打印它,以查看代码是否捕获了所需的输出”
Cisco交换机上的ssh端口已关闭。由于“遗留”原因,某些设备不支持telnet。有时,这些遗留原因是企业文化固有的。啊。。。技术。可能Cisco编写
用户名:
或登录:
,但您的代码正在等待登录:
。使用print
在屏幕上为您写入更多信息,如“现在我将等待‘登录:’”等。请您详细解释最后3行。或者如果主机名是其他的东西呢。请你详细说明一下。谢谢,所以当您通过telnet登录路由器时,您必须考虑这一点。确保可以从命令行执行telnet 10.203.4.1
,然后必须获得用户名:
提示,然后是密码:
提示等。如果成功登录,您将能够看到通过telnet收到的下一个提示。在上面的程序中,我手动将主机名设置为“deep”
——您可以使用任何您喜欢的东西。下一个child.expect
基本上希望看到一个带有主机名的提示。有没有任何方法可以通过pexpect从交换机获取主机名?谢谢
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()
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")