This article shares how to use python to Detect ip addresses used and unused ip addresses in a CIDR block. I learned from my previous blog and added many things on my own.
The subprocess module is used.
>>> Import subprocess
>>> P = subprocess. Popen ('df-H', stdin = subprocess. PIPE, stdout = subprocess. PIPE, stderr = subprocess. PIPE, shell = True)
# Obtain the return code of the command execution result through the wait () function
>>> P. wait ()
0
# Obtain the command output result (standard output) by using the read () method
>>> P. stdout. read ()
'Filesystem Size Used Avail Use % Mounted on \ n/dev/sda1 18G 11G 5.8G 65%/\ ntmpfs 495 M 0 495 M 0%/dev/shm \ n'
# Obtain the output result of a command error using the read () method
>>> P. stderr. read ()
''
# Null, indicating 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'
@ Other methods for obtaining error output include read (), readline (), readlines (), close (), write (), and writelines.
#! /Usr/bin/env python
# _ * _ Coding: utf8 _*_
# By lijiajun
Import re, subprocess, OS, sys
Net_region = '2017. 192'
Print ("##################################### ####################")
Print ("# This script is mainly based on ping to test the ip addresses used and unused ip addresses of a network segment #")
Print ("# save them to/tmp/alive_ip.txt #")
Print ("# and/tmp/dead_ip.txt #")
Print ("##################################### ####################")
Print ("")
If OS. 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 ('2017 packet loss', result)
If len (regx) = 0:
Print ("\ 033 [1; 32; 40 m % s is UP \ 033 [0 m") % (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
Return alive_ip
Else:
Print "\ 033 [31 m % s is DOWN \ 033 [0 m" % (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 describes how to use python to Detect ip addresses used and unused ip addresses in a CIDR block. For more information, see other related articles in the first PHP community!