0

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")

Telnet:

tn = telnetlib.Telnet(IP)
tn.write(command+"\r\n")
f=open(filename,w)
while True:
 response = tn.read_until("\n")
 f.write(response)

Here, In between IP goes down. During that time i need to ping that IP & whenever it comes up i need to start collecting logs again. How can i do this ?

Actually, I need to collect logs through telnet (which is indefinite). I could able to do it. During this process, IP from which i'm collecting logs goes down. So, I need to track on this IP (Ping). Whenever IP comes up again, I've to start collecting the logs again. It would be great, if you can help me on this.

I found the solution:

tn=telnetlib.Telnet(IP)
    tn.write(command+"\r\n")
    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.
Jackie James
  • 785
  • 8
  • 15
  • 28
  • "hide console" seems like a separate issue (there are already several questions about it on StackOverflow). If you found a satisfactory solution; post it as an answer instead of editing the question. – jfs Jan 17 '15 at 20:19
  • what happens if the host is down during `tn.read_until("\n")` call? – jfs Jan 17 '15 at 20:20
  • @ Sebastin: If the host is down, then it follows the else part doesn't execue while block. So, it doesn't encounter tn.read_until("\n") . – Jackie James Jan 18 '15 at 01:13
  • What happens if you remove `os.system()` and allow `tn.read_until("\n")` to run anyway? – jfs Jan 18 '15 at 01:15

1 Answers1

0

Maybe pexpect is what you're looking for?

This snippet will start a ping process, and block until it sees the output "bytes from" or until it times out (1 minute by default):

import pexpect

def wait_until_online(host, timeout=60):
    child = pexpect.spawn("ping %s" % host)
    child.expect("bytes from", timeout)
André Laszlo
  • 15,169
  • 3
  • 63
  • 81
  • Timing can be anything. Not only a minute. – Jackie James Jan 16 '15 at 11:51
  • You could set it to something higher, like 24 hours = 86400 seconds. `wait_until_online("example.com", timeout=86400)` – André Laszlo Jan 16 '15 at 11:54
  • If you _really_ want it to run indefinitely, you can catch the `pexpect.TIMEOUT` exception and simply try again. – André Laszlo Jan 16 '15 at 11:58
  • Thank you André Laszlo. I'm sorry, if i have not been clear above. Actually, I need to collect logs through telnet (which is indefinite). I could able to do it. During this process, IP from which i'm collecting logs goes down. So, I need to track on this IP (Ping). Whenever IP comes up again, I've to start collecting the logs again. It would be great, if you can help me on this. – Jackie James Jan 16 '15 at 14:09
  • Try something like: 1) collect logs normally, 2) catch any exceptions related to the host going down (timeouts etc), 3) use the function above to wait for the host to resurrect 4) goto 1 – André Laszlo Jan 16 '15 at 14:31
  • @ André Laszlo : I have pasted a solution above. Could you please help me on that ? – Jackie James Jan 17 '15 at 14:44