Python:处理同一IP上的多个telnet会话
我几乎没有任何数量的命令列表,这些命令必须在一个特定的IP/主机上通过telnet执行。以及要存储在单独文件中的输出。这些命令特定于日志收集Python:处理同一IP上的多个telnet会话,python,telnet,telnetlib,Python,Telnet,Telnetlib,我几乎没有任何数量的命令列表,这些命令必须在一个特定的IP/主机上通过telnet执行。以及要存储在单独文件中的输出。这些命令特定于日志收集 I need them in such a way that, Execute all those required commands at once (Start/enabling for log collection) - Multiple telnet sessions, one session per command. After sometim
I need them in such a way that, Execute all those required commands at once (Start/enabling for log collection) - Multiple telnet sessions, one session per command. After sometime (Not a timed activity), require another script to stop all of them & logs stored in separate file respectively (based on the list of commands executed).
I could able to do it only for one particular command, that too only for short interval of time.
我希望我对细节很清楚。如果你不清楚这个概念,请告诉我。请在这方面帮助我
import sys
import telnetlib
import time
orig_stdout = sys.stdout
f = open('out.txt', 'w')
sys.stdout = f
try:
tn = telnetlib.Telnet(IP)
tn.read_until(b"login: ")
tn.write(username.encode('ascii') + b"\n")
tn.read_until(b"# ")
tn.write(command1.encode('ascii') + b"\n")
#time.sleep(30)
z = tn.read_until(b'abcd\b\n',4) >> Just a random pattern, so that it reads for long duration.
#z=tn.read_very_eager()
output = z.splitlines( )
except:
sys.exit("Telnet Failed to ",IP)
for i in output:
i=i.strip().decode("utf-8")
print(i)
sys.stdout = orig_stdout
f.close()
你试过什么?你在哪里失败了?@Doon:我在上面提供了一个片段。它只适用于单个命令,我无法长时间收集日志。这是适用于多个命令&应该能够收集,只要我愿意。通常,一个脚本开始执行命令,另一个脚本停止命令。那么,到底是什么失败了?错误是什么?连接是否关闭?如果没有来自telnet的响应,连接将失败。在某些情况下,它将暂停一段时间,然后再次开始显示日志。您当前的read_until显示为read_,直到看到abcd或4秒。超时是您想要的吗?