非标准端口上的Python telnetlib提示

时间:2022-02-23 22:25:36

Network guy playing with coding here

网络家伙在这里玩编码

I've a very basic script that telnets to a switch/router, runs a few commands and terminate. Everything is done on port 23. The problem I'm having is that when I connect to a device through a term server or to a virtual router(csr1000v) on an ESXi host,through a non-standard port, I don't get a prompt and I can't run the commands.

我有一个非常基本的脚本telnet到交换机/路由器,运行一些命令并终止。一切都在端口23上完成。我遇到的问题是,当我通过术语服务器或ESXi主机上的虚拟路由器(csr1000v)连接到设备时,通过非标准端口,我无法获得提示,我无法运行命令。

In SecureCRT I can connect to them without a problem, but I must hit enter before I am presented with a prompt. In a normal switch/router the prompt comes up immediately. I've tried putting in telnet.write(b" \r\n") once the connection is made but it made no difference.

在SecureCRT中,我可以毫无问题地连接到它们,但是在出现提示之前我必须按Enter键。在正常的交换机/路由器中,提示立即出现。一旦建立连接,我就尝试输入telnet.write(b“\ r \ n”),但没有区别。

I've also set it up that if I bring up the prompt through SecureCRT, disconnect and then run the script, it executes successfully. I've read the telnetlib docs without anything that looks like it would help

我还设置了如果我通过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)

1 个解决方案

#1


It's not regular telnet. This is serial over telnet. You should send spec. telnet data first.

这不是常规的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()

#1


It's not regular telnet. This is serial over telnet. You should send spec. telnet data first.

这不是常规的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()