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 % 


72 views0 comments

Recent Posts

See All

Python | Getting pictures from Instagram

In this post you will learn how you can save pictures from Instagram account in your PC using Python. You just need to install "instalooter" into your Python packages and run the command. There are ma

PowerShell ISE | Multiple Ping

The Windows PowerShell Integrated Scripting Environment (ISE) is a graphical user interface and front-end hosting application for Windows PowerShell. The ISE enables developers to run PowerShell comma

Telegram message with Python

In this post I am showing how to send text message and photo from any computer to mobile telegram app. This is only few lines of code to send automatic message to smartphone from raspberry pi or from