Skip to content

Commit

Permalink
Updated to work with Windows
Browse files Browse the repository at this point in the history
On Windows, it returns "active" even for hosts that are down but it doesn't matter since the goal is refresh the ARP cache.
  • Loading branch information
rikosintie authored Apr 29, 2022
1 parent dbd1452 commit 002cae2
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions pinger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
https://www.tutorialspoint.com/python3/python_exceptions.htm
'''

import os
import time
# import os
# import time
import ipaddress
import platform
from subprocess import Popen, DEVNULL, PIPE
import subprocess
import shlex
print()
# platform_name = os.uname()
System_name = platform.system()
Expand Down Expand Up @@ -72,14 +73,17 @@
# print(i)
# p[i] = subprocess.Popen(['ping', '-n', '-w5', '-c3', i], stdout=DEVNULL, stderr=subprocess.STDOUT)
# ran in python 3.8.x
if System_name == "Darwin" or "Linux":
cmd = "ping"
cmd_args = "-c 3"
if System_name == "Darwin" or System_name == "Linux":
cmd = "ping -n -c 3 " + i
args = shlex.split(cmd)
else:
cmd = "ping"
cmd_args = "-n 3"
cmd = "ping -n 3 " + i
args = shlex.split(cmd)
shell_needed = True
# print("Args", args)
# subprocess.run([cmd, cmd_args])
p[i] = subprocess.Popen([cmd, cmd_args, i], stdout=DEVNULL, stderr=subprocess.STDOUT)
p[i] = subprocess.Popen(args, stdout=subprocess.DEVNULL, \
stderr=subprocess.STDOUT)

# p[i] = subprocess.Popen(['ping', '-n', '-c3', i], stdout=DEVNULL, stderr=subprocess.STDOUT)
# NOTE: you could set stderr=subprocess.STDOUT to ignore stderr also
Expand All @@ -91,6 +95,7 @@
for ip, proc in p.items():
if proc.poll() is not None: # ping finished
del p[ip] # remove from the process list
# print(proc.returncode)
if proc.returncode == 0:
print('%s active' % ip)
elif proc.returncode != 0:
Expand Down

0 comments on commit 002cae2

Please sign in to comment.