PING:
import os
ip=1.1.1.1
o=os.system("ping "+ip)
time.sleep(10)
print(o)
if res == 0:
print(ip,"is active")
远程登录:
tn = telnetlib.Telnet(IP)
tn.write(command+"rn")
f=open(filename,w)
while True:
response = tn.read_until("n")
f.write(response)
在这里,在IP之间下降。在那段时间里,我需要ping该IP,每当它出现时,我都需要再次开始收集日志。我该怎么做?
实际上,我需要通过telnet(无限期)收集日志。我能做到。在此过程中,我从中收集日志的 IP 会关闭。所以,我需要跟踪这个IP(Ping)。每当 IP 再次出现时,我都必须再次开始收集日志。如果你能在这方面帮助我,那就太好了。
我找到了解决方案:
tn=telnetlib.Telnet(IP)
tn.write(command+"rn")
f=open(filename,w)
while (os.system("ping -n 1 IP") == 0):
response = tn.read_until("n")
f.write(response)
else:
call some module for telnetting again/goto
But, here is it possible to hide the console when we use (os.system(ping)). I know it can be done through subprocess. But since os.system is a one liner & very easy to verifY the result also.
也许 pexpect 是你要找的?
此代码段将启动一个 ping 进程,并阻止直到它看到输出"字节来自"或直到超时(默认为 1 分钟):
import pexpect
def wait_until_online(host, timeout=60):
child = pexpect.spawn("ping %s" % host)
child.expect("bytes from", timeout)