Skip to content

Instantly share code, notes, and snippets.

@tranductam2802
Created May 27, 2019 01:44
Show Gist options
  • Save tranductam2802/6ae638d30f8e9835f579cd98bdbf819b to your computer and use it in GitHub Desktop.
Save tranductam2802/6ae638d30f8e9835f579cd98bdbf819b to your computer and use it in GitHub Desktop.
Ping all local network for finding avaiable IP address
import subprocess
import ipaddress
from subprocess import Popen, PIPE
network = ipaddress.ip_network('192.168.0.0/24')
for idx in network.hosts():
ip = str(idx)
toping = Popen(['ping', '-w', '300', ip], stdout=PIPE)
output = toping.communicate()[0]
hostalive = toping.returncode
if hostalive == 0:
print(ip, 'avaiable')
else:
print(ip, 'not avaiable')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment