top of page

Python | Subnet Ping

Sep 18, 2020
1 min read

This script will ping and display status of all hosts in a subnet we provide.

import subprocess
import ipaddress
from subprocess import Popen, PIPE
subnet = input("Please enter the network: ")
network = ipaddress.ip_network(subnet)
for i in network.hosts():
    i=str(i)
    toping = subprocess.Popen(['ping', '-c', '3', i], stdout=PIPE)
    output = toping.communicate()[0]
    hostalive = toping.returncode
    if hostalive == 0:
        print(i,'is ' + '\033[92m' + 'reachable' + '\033[0m')
    else:
        print(i,'is ' + '\033[91m' + 'unreachable' + '\033[0m')

That's what the script works:

drm@STENGE % python3 ping-subnet.py
Please enter the network: 192.168.0.0/29
192.168.0.1 is reachable
192.168.0.2 is unreachable
192.168.0.3 is reachable
192.168.0.4 is reachable
192.168.0.5 is reachable
192.168.0.6 is reachable
drm@STENGE % 


Recent Posts

See All
Hiding Messages Inside an Emoji

Imagine receiving a message that contains nothing more than this: 🌊‌‌‌‌‌‌‌‌‌‍‌‍‍‌‍‍​‌‌‌‌‌‌‌‌‌‍‌‍‌‌‍‍​‌‌‌‌‌‌‌‌‌‍‌‍‌‍‌‌​‌‌‌‌‌‌‌‌‌‍‌‌‌‍‌‍​‌‌‌‌‌‌‌‌‌‍‌‌‍‍‍‌​‌‌‌‌‌‌‌‌‌‍‌‌‌‍‍‍​‌‌‌‌‌‌‌‌‌‍‌‌‌‍‌‍​‌‌‌‌‌‌‌‌‌‌‍‌‍

 
 
 
Fake Data in Python

If you are developing applications, testing your software, or just exploring Python's capabilities, you might have faced a situation...

 
 
 
Mobile | Secret Phone Codes

Our smartphones have become extensions of ourselves, facilitating much of our communication and storing troves of private data. If...

 
 
 

Comments


STENGE.info | Built with curiosity. Secured by knowledge.

bottom of page