Python | Subnet Ping

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 % 


103 views0 comments

Recent Posts

See All

Run Python Automation Script to fetch Cisco Routers and Switches details in Bulk. The details includes “hostname, uptime, current version, current image, serial number, device model, device memory” Pr

Here I am going to show you an example how to encrypt PDF to make it password protected using PyPDF2 module in Python programming language. Piece of cake 🍰 This example we are going to read the exist

Most of us have seen the dreaded “No Internet” error message on Google Chrome. You can actually turn this screen into a fun, dino-themed endless runner game and, even better, hack the hidden Easter eg