2

I'm trying to write a script that checks if a host on my intranet is up. if so, wait 10 seconds and test again. if it is down, send a wake on lan packet to the host, then test again in 10 seconds. The code compiles but doesn't seem to be working. Any help is appreciated.

import os
import socket
def main():
    hostname = "10.0.0.5" 
    response = os.system("ping -c 1 " + hostname)
    if response == 0:
        print ("Host " + hostname + "is up.")
        Time.Sleep(10)
        main()
    else:        
        print("Host " + hostname + "is down.")
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.sendto('\xff'*6 + '\x00\x21\x6A\xC7\x1A\x42'*16, ('10.0.0.5', 80))
        Time.Sleep(10)
        main()

Update: I changed the if conditional to != with the 10.0.0.5 host on to test if it's sending the packet, and it's not (confirmed with wireshark). I don't know if it's even running my code tbh.

New Code seems to be working, only issue is that it's ignoring time.sleep and just repeating once the ping finishes

import os
import socket
import subprocess
import time
from time import sleep
x = 0
while x < 1:
        hostname = "10.0.0.5"
        output = subprocess.Popen(["ping.exe",hostname],stdout = subprocess.PIPE).communicate()[0]
if ('unreachable' in output):
        print hostname, 'is down.'
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.sendto('\xff'*6 + '\x00\x21\x6A\xC7\x1A\x42'*16, ('10.0.0.255', 80))
        time.sleep(10)
else:
        print hostname, 'is up.'
        time.sleep(10)
x = x + 0
dan bob
  • 29
  • 3
  • possible duplicate of [Assign output of os.system to a variable and prevent it from being displayed on the screen](http://stackoverflow.com/questions/3503879/assign-output-of-os-system-to-a-variable-and-prevent-it-from-being-displayed-on) – Nir Alfasi Oct 26 '14 at 06:37
  • 1
    what? that's not even relevant to my problem. – dan bob Oct 26 '14 at 06:43
  • Did you forget to call the `main()` within your script ? – Himal Oct 26 '14 at 06:57
  • @alfasin.I don't think it's relevant to this question. – Himal Oct 26 '14 at 06:59
  • @Himal yes it is: he's trying to read the output from `os.system` but that's not the way to do it... – Nir Alfasi Oct 26 '14 at 07:19
  • 1
    @alfasin.But this is different.OP is trying to get the return code NOT the output. – Himal Oct 26 '14 at 07:26
  • Hi! If the host is down, it usually has no IP adress. So, where should the router send the packet? Therefore, the packet has to be sent to *all* available computers via a broadcast. Try 10.0.0.255 or whatever the broadcast Adresse is. Btw: 10s is far to less if the host has to boot. – sweber Oct 26 '14 at 08:28
  • 1
    Calling main() from within main() is a not such a good idea, because each time it is called it pushed to the stack (see recursive calls). In the end the stack will be filled up, and you'll get an exception. This is however not the question you asking about, but just a problem in the code :) – J. P. Petersen Oct 26 '14 at 10:41
  • @Himal check out his update... – Nir Alfasi Oct 26 '14 at 17:33

1 Answers1

0

The indentations are off in your updated code.also, looking for 'unreachable' in the output wouldn't be the best thing to do.what if it timed out or showed another error ? i would use the return code instead.

Here is an updated version.make sure to preserve the indentations.

import os
import time
import socket
import subprocess

hostname = "10.0.0.5"

while 1:
    sp = subprocess.Popen(["ping.exe", hostname], stdout = subprocess.PIPE)

    sp.wait() # Wait for ping.exe to terminate.

    return_code = sp.returncode # Get the return code

    if return_code != 0:
        print hostname, 'is down.'
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        #I'm not that familiar with this part.assuming yours is correct.
        s.sendto('\xff'*6 + '\x00\x21\x6A\xC7\x1A\x42'*16, ('10.0.0.255', 80))
    else:
        print hostname, 'is up.'

    time.sleep(10) # Sleep for 10 seconds
Himal
  • 1,351
  • 3
  • 13
  • 28