


How to use python to detect used IP and unused IP of a certain network segment
I learned from the blogs of my predecessors and then added a lot of things myself.
The subprocess module is used
>>> import subprocess
>>> p = subprocess.Popen('df -h',stdin =subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
#Get the return code of the command execution result through wait()function
>>> p.wait()
0
#Get the command output result (standard output) through the read() method
> >> p.stdout.read()
'Filesystem Size Used Avail Use% Mounted on\n/dev/sda1 18G 11G 5.8G 65% /\ntmpfs 495M 0 495M 0% /dev/shm \n'
#Get the command error output result through the read() method
>>> p.stderr.read()
''
# is empty, indicating that there is no error output
#Get error output
> ;>> p = subprocess.Popen('ls /etc/password',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True,close_fds=True)
>>> p.stderr.read()
'ls: cannot access /etc/password: No such file or directory\n'
@Get other error output There are also methods: read(), readline(), readlines(), close(), write() and writelines(), etc.
#!/usr/bin/env python
#_*_ coding:utf8 _*_
# by lijiajun
import re,subprocess,os,sys
net_region='192.168.3'
print("
#")
print("#This script is mainly based on ping, testing a certain Network segment used ip and unused ip #" /dead_ip.txt #")
print("
#")
print(" ")
ifos .path.isfile("/tmp/alive_ip.txt"):
os.popen("mv /tmp/alive_ip.txt /tmp/alive_ip.txt.old")
print "you can see the used ip in this file : /tmp/alive_ip.txt"
if os.path.isfile("/tmp/dead_ip.txt"):
os.popen ("mv /tmp/dead_ip.txt /tmp/dead_ip.txt.old")
print "you can see the unused ip in this file : /tmp/dead_ip.txt"
print(" ")
dead_ip=0
alive_ip=0
def check_alive(ip,
count,
timeout ):
global alive_ip
global dead_ip cmd='ping -c %d -w %d %s' % (count,timeout,ip)
p=subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
result=p.stdout.read()
regx=re.findall('100% packet loss',result)
if len(regx)==0:
print("\033[1;32;40m %s is UP \033[0m") % (ip)
f=file(' /tmp/alive_ip.txt','a') f.write('%s\n' %ip)
f.close()
alive_ip= alive_ip+1 print "alive_ip count is %d" % alive_ip
returnalive_ip
else###:### ### print "\033[31m %s is DOWN\033[0m" % (ip)###### f=file('/tmp/dead_ip.txt','a')##### # f.write('%s\n' %ip)###### f.close()###### dead_ip=dead_ip+1###### print "dead_ip count is %d" % dead_ip###### return dead_ip######if name=="###main###":###### #f=file('/tmp/iplist.txt') ###### ###for### i in ###range###(1,255):###### ip='%s.%s' % (net_region,i)### ### print ip###
check_alive(ip,1,1)
print (" ")
print "final dead_ip count is %d" % dead_ip
print "final alived_ip count is %d" % alive_ip
The above is the detailed content of How to use python to detect used IP and unused IP of a certain network segment. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
