I am learning Python and one thing I thought I could work on was a program to scan an IP range and then compare the results with previous - alerting me to any new IP addresses that respond to ping.

Can I get some feedback on my code? Is there a better way? Is there a more Pythonic way?

All it does so far is scan a range and build a list of IPs that replied to ping.

Any help will be appreciated.


import os

hosts = []
subnet= "10.0.0."

for host in range (1,4):
    ip = subnet + str(host)
    ping_cmd = "ping -c1 -w1 " + ip + " > /dev/null"
    responce = os.system(ping_cmd)
    if responce == 0:
        print(ip +" is live")
        print(ip + " is dead")

print hosts

I read (somewhere) that using os.system like that was deprecated and that a better way was to use subprocess, so I re-wrote it.
It functions - it finds "live" IP addresses.
Is there a better, more Pythonic way to do this?


import subprocess

hosts = []
subnet = "10.0.0."
start_ip = 1
stop_ip = 6

def find_live_hosts(subnet,start_ip,stop_ip):
    for host in range(start_ip,stop_ip):
        ip = subnet + str(host)
        response = subprocess.call(['ping', '-c3', '-w3', ip], stdout=subprocess.PIPE)
        if response == 0:
            print("{0} is live").format(ip)
    return hosts

live_hosts = find_live_hosts(subnet,start_ip,stop_ip)