import subprocess for ping in range(1,10): address = "192.168.2." + str(ping) res = subprocess.call(['ping', '-c', '3', address]) if res == 0: print( "ping to", address, "OK") else: print("ping to", address, "failed!")