Python - проверка пинга всей подсети - sirken/ping-subnet

Материал из Wiki - Iphoster - the best ever hosting and support. 2005 - 2024
Перейти к:навигация, поиск

Python - проверка пинга всей подсети - sirken/ping-subnet

https://github.com/sirken/ping-subnet


# vi ping-subnet.py
import ipaddress
from subprocess import Popen, PIPE, DEVNULL
# IP range
network = ipaddress.ip_network('XX.XX.XX.0/24')
# Time between pings (ms)
delay = '50'
# Because it looks cool
def spinning_cursor():
   while True:
       for cursor in '|/—\\':
           yield cursor
spinner = spinning_cursor()
# Loop through subnet hosts
for i in network.hosts():
   i=str(i)
   octets = i.split(".")
   octet4 = octets[3]
   # fping command
   try:
       toping = Popen(['fping', '-c1', '-t', delay, '-q', i], stdout=DEVNULL, stderr=PIPE)
   except:
       print("fping is required")
       break
   output = toping.communicate()[0]
   hostalive = toping.returncode
   if hostalive == 0:
       print(i,'is ' + '\033[92m' + 'reachable' + '\033[0m')
   else:
       print(next(spinner), str(octet4), end="\r", flush=True)
       print(i,'is ' + '\033[91m' + 'unreachable' + '\033[0m')


# apt install fping

Заменить

 network = ipaddress.ip_network('XX.XX.XX.0/24')

на свою подсеть /24

Запуск:

# python3 ping-subnet.py