Python script that pings an internet server and let's you know when you are reconnected

Question:

I am trying to make a script that lets you know when a server connection is up and running again after going offline. I am using ping as a function in the os.sytem (so it runs in the clients cmd prompt). My thoughts was that if the result of the ping is positive, it will print("!!! --servername-- is up and running again!!!"). If the ping result is negative, the script should run the response() function again. Right now the ping function works, but the if statement with result does not give any output value.

import os
import time


ip_list = "8.8.8.8"
result = ()

if os.name == "nt":
    def response():
        os.system("ping -w 2s " + ip_list)
        return(result)
        #If the os name is nt (Windows), then the command should be as the above
else:
    def response():
        os.system("ping -i 2s " + ip_list)
        return(result)
        #If the os name is something else (Linux), then the command should be as the above

if result == 0:
    print (("!!! ") + ip_list + (" IS UP AND RUNNING AGAIN !!!"))
    #If the result of the ping is positive (== 0), then print the above message
elif result != 0:
    response()
    #If the result is not positive (not equal to 0), then do the ping function again
else:
    print(" ... ERROR ... ")

Also thinking about adding time.sleep(20) at the end so I have the time to view the eventually printed message.

Asked By: Hackerman

||

Answers:

You have few mistakes.


First: you have to assing result to variable

def response():
    result = os.system("ping -w 2s " + ip_list)
    return result

or you could use it directly with return

def response():
    return os.system("ping -w 2s " + ip_list)

Second: you have to run this function and get result

result = response()

if result == 0:
   # ... code ...

Third: it is preferred to send all values as arguments

def response(ip):
    return os.system("ping -w 2s " + ip)

result = response(ip_list)

if result == 0:
   # ... code ...

Fourth: result can be equal 0 or not equal 0 – there is no other option – so using else is useless

if result == 0:
    print (("!!! ") + ip_list + (" IS UP AND RUNNING AGAIN !!!"))
else:
    response()

Fifth: if you want it check again and again then you should use while-loop.

while True:
    result = response(ip_list)

    if result == 0:
        # if the result of the ping is positive (== 0), then print message
        print(("!!! ") + ip_list + (" IS UP AND RUNNING AGAIN !!!"))
        break  # exit loop

BTW: it is preferred to put comment in line beforeof code, not in line after code. Eventually at the end of line of code. And it is preferred to put single space after # (and two space before # when it is in the same line.)

See more: PEP 8 — Style Guide for Python Code


import os
import time

# --- functions ---  # PEP8: functions before main code

if os.name == "nt":
    def response(ip):
        # if the os name is nt (Windows)
        return os.system("ping -w 2s " + ip)
else:
    def response(ip):
        # if the os name is something else (Linux)
        return os.system("ping -i 2s " + ip)

# --- main ---

ip_list = "8.8.8.8"

while True:
    result = response(ip_list)

    if result == 0:
        # if the result of the ping is positive (== 0), then print message
        #print("!!! " + ip_list + " IS UP AND RUNNING AGAIN !!!")  # there is no need to put string in `( )`
        print(f"!!! {ip_list} IS UP AND RUNNING AGAIN !!!")  # f-string can be more readable.
        break  # exit loop
    
    time.sleep(5) 
Answered By: furas