Python Ping IP address

Working with network devices it’s good to check if the IP address is available (in case ICMP is allowed/enabled) before trying to get into it.

Here is a quick way I found which should work in a script for both Linux and Windows:

import subprocess
import platform

def ping_ip(current_ip_address):
        try:
            output = subprocess.check_output("ping -{} 1 {}".format('n' if platform.system().lower(
            ) == "windows" else 'c', current_ip_address ), shell=True, universal_newlines=True)
            if 'unreachable' in output:
                return False
            else:
                return True
        except Exception:
                return False

if __name__ == '__main__':
    current_ip_address = ['8.8.8.8', '8.8.4.4', '1.2.3.4']
    for each in current_ip_address:
        if ping_ip(each):
            print(f"{each} is available")
        else:
            print(f"{each} is not available")

Windows:

p.s.: works the same way via CMD

Linux:

Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *