我通过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()
运行脚本后,它只是挂断了。我该如何解决?
最佳答案
这是一个更简单的解决方案:
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')
等等。
关于python - 使用python的Telnet cisco交换机,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19671936/