38 lines
1.2 KiB
Python
Executable File
38 lines
1.2 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import ipaddr
|
|
import os
|
|
import socket
|
|
import subprocess
|
|
import sys
|
|
|
|
def main():
|
|
for arg in sys.argv[1:]:
|
|
try:
|
|
net = ipaddr.IPv4Network(arg)
|
|
except ipaddr.AddressValueError:
|
|
sys.stderr.write('Invalid network address: {}\n'.format(arg))
|
|
else:
|
|
for ip in net.iterhosts():
|
|
p = subprocess.Popen(['ping', '-c1', '-W1', str(ip)],
|
|
stdout=open(os.devnull, 'w'),
|
|
stderr=open(os.devnull, 'w'),
|
|
stdin=open(os.devnull))
|
|
try:
|
|
p.wait()
|
|
except KeyboardInterrupt:
|
|
print()
|
|
raise SystemExit
|
|
try:
|
|
hostname = '{} ({})'.format(
|
|
ip,
|
|
socket.gethostbyaddr(str(ip))[0],
|
|
)
|
|
except socket.herror:
|
|
hostname = ip
|
|
state = 'in use' if p.returncode == 0 else 'not responding'
|
|
print('{} is {}'.format(hostname, state))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|