Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

ping all IP addresses in a network

# Import modules
import subprocess
import ipaddress

# Prompt the user to input a network address
net_addr = input("Enter a network address in CIDR format(ex.192.168.1.0/24): ")

# Create the network
ip_net = ipaddress.ip_network(net_addr)

# Get all hosts on that network
all_hosts = list(ip_net.hosts())

# Configure subprocess to hide the console window
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE

# For each IP address in the subnet, 
# run the ping command with subprocess.popen interface
for i in range(len(all_hosts)):
    output = subprocess.Popen(['ping', '-n', '1', '-w', '500', str(all_hosts[i])], stdout=subprocess.PIPE, startupinfo=info).communicate()[0]
    
    if "Destination host unreachable" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")
    elif "Request timed out" in output.decode('utf-8'):
        print(str(all_hosts[i]), "is Offline")
    else:
        print(str(all_hosts[i]), "is Online")
Comment

PREVIOUS NEXT
Code Example
Python :: date format flouytter 
Python :: cx_freeze include images in specific path 
Python :: django auto complete light styling 
Python :: the grandest staircase of them all foobar solution 
Python :: linear zoeken python 
Python :: Kinesis Client put_record 
Python :: sonido sfr200a 
Python :: permcheck codility python 
Python :: afkastiningsgrad 
Python :: dictionary changed size during iteration after pop function 
Python :: check version of various pkgs 
Python :: Scope, Global Variables and Global Keyword 
Python :: .comments.all order django 
Python :: update cell in sheet by column name using pandas 
Python :: pandas drop unnamed columns grebber 
Python :: function continuity python 
Python :: File "main.py", line 11 if message.author == client.user: ^ IndentationError: expected an indented block 
Python :: filter dataframe site:stackoverflow.com 
Python :: ipynb to py online converter 
Python :: how to multiply two lists in python 
Python :: convert html to python 
Python :: How to change the height of an input in python tkinter 
Python :: stop animation matplotlib 
Python :: Walrus operator in list comprehensions [Python 3.8.0] 
Python :: qaction disacble python 
Python :: python average function program 
Python :: python to pseudo code converter 
Python :: how to launch a application using python 
Python :: how to randomise a string in python 
Python :: range() in python 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =