DeaD_EyE

gateway_alive

May 8th, 2020
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. import platform
  2. import shlex
  3. from subprocess import call, DEVNULL
  4. from netifaces import gateways, AF_INET
  5.  
  6. def gateway_alive(nic="wlan0"):
  7.     system = platform.system()
  8.     gws = gateways().get(AF_INET, [])
  9.     for ip, interface, default in gws:
  10.         if interface == nic:
  11.             if system == "Windows":
  12.                 cmd = shlex.split(f"ping -n 1 -w 1 {ip}")
  13.             elif system == "Linux":
  14.                 cmd = shlex.split(f"ping -c1 -W1 {ip}")
  15.             else:
  16.                 raise RuntimeError(f"{system} is not supported.")
  17.             if call(cmd, stdout=DEVNULL, stderr=DEVNULL) == 0:
  18.                     return True
  19.             else:
  20.                     return False
  21.     return False
Add Comment
Please, Sign In to add comment