网络人在这里玩编码
我有一个非常基本的脚本telnet到交换机/路由器,运行一些命令并终止。一切都在端口23上完成。我遇到的问题是,当我通过术语服务器或ESXi主机上的虚拟路由器(csr1000v)连接到设备时,通过非标准端口,我无法获得提示,我无法运行命令。
在SecureCRT中,我可以毫无问题地连接到它们,但是在出现提示之前我必须按Enter键。在正常的交换机/路由器中,提示立即出现。 一旦建立连接,我就尝试输入telnet.write(b“\ r \ n”),但没有区别。
我还设置了如果我通过SecureCRT提示提示,断开连接然后运行脚本,它就会成功执行。 我已经阅读了telnetlib文档而没有任何看起来有帮助的文件
telnet = telnetlib.Telnet(HOST,port)
#telnet.write(b" \r\n")
telnet.read_until(b"Username: ")
telnet.write(user.encode('ascii'))
telnet.read_until(b"Password: ")
telnet.write(password.encode('ascii'))
telnet.read_until(b">")
telnet.write(b"enable\r\n")
telnet.read_until(b"Password: ")
telnet.write(enable_password.encode('ascii'))
telnet.read_until(b"#")
telnet.write(b"term length 0\r\n")
telnet.read_until(b"#")
response = send_command(telnet,"sh run")
print (response)
response = send_command(telnet," sh ip int br | ex una")
print (response)
答案 0 :(得分:0)
这不是常规的telnet。这是通过telnet串行的。你应该发送规范。先telnet数据。
import time
import telnetlib
cmd_ser1 = '\xff\xfc\x25'
cmd_ser2 = '\xff\xfb\x00'
cmd_ser3 = '\xff\xfd\x00\xff\xfb\x03\xff\xfd \x03\xff\xfd\x01\xff\xfe\xe8'
cmd_ser4 = '\xff\xfe\x2c'
cmd1 = "enable"
cmd2 = "cisco"
cmd3 = "copy running-config flash:/test.cfg"
cmd4 = "exit"
promt = "%s>"%(host)
host_disable = "%s>"%(host)
tn = telnetlib.Telnet(addr,port, timeout=5)
tn.set_debuglevel(100)
tn.write(cmd_ser1)
time.sleep(1)
tn.write(cmd_ser2)
time.sleep(1)
tn.write(cmd_ser3)
time.sleep(1)
tn.write(cmd_ser4)
time.sleep(1)
tn.read_very_eager()
if promt == "host_disable":
tn.write(cmd1.encode('ascii') + b"\n")
time.sleep(1)
tn.read_very_eager()
tn.write(cmd2.encode('ascii') + b"\n")
time.sleep(2)
tn.read_very_eager()
tn.write(cmd3.encode('ascii') + b"\n")
tn.read_very_eager()
tn.write(b"\n")
time.sleep(2)
tn.write(b"\n")
time.sleep(2)
tn.write(cmd4.encode('ascii') + b"\n")
tn.read_very_eager()
tn.close()
else:
tn.write(cmd3.encode('ascii') + b"\n")
tn.read_very_eager()
tn.write(b"\n")
tn.write(b"\n")
time.sleep(2)
tn.write(cmd4.encode('ascii') + b"\n")
tn.read_very_eager()
tn.close()