How to use python to Detect ip addresses used and unused ip addresses in a CIDR block

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.