Python script implementation of corporate office network IP paragraph usage

Source: Internet
Author: User
Tags python script

Because the company is using VMware virtualization technology, each time the virtual machine to allocate a connection to the external network IP address, in addition to the company scale, the new people also need to assign IP address, it is easy to cause the IP address of the bad and IP address conflict,

So we decided to use a script to troubleshoot the idle usage of IP, reduce the time cost of operations, and less IP collisions:

The script reads as follows:

If you want to use an IP address that can be modified to your own company:


#/usr/bin/env Python

#-*-conding:utf-8-*-

Import subprocess

Import threading

Import time

Import re


Ip_num = 256

List_ping_result = []



Class Pingthread (threading. Thread):

def __init__ (self, str_ip, Sleep_time, G_list_p_r):

Threading. Thread.__init__ (self)

Self.sleep_time = Sleep_time

SELF.STR_IP = Str_ip

Self.list_p_r = G_list_p_r


def run (self):

Time.sleep (Self.sleep_time)

Ftp_sub = subprocess. Popen ("Ping%s-n 3"% self.str_ip,

Stdin=subprocess. PIPE, Stdout=subprocess. PIPE, Shell=true)

ret = Ftp_sub.stdout.read ()

Str_ret = Ret.decode ("GBK")

ret_s = Re.search ("TTL", Str_ret)

If ret_s:

Self.list_p_r.append (' ping ok ', self.str_ip)

Else

Self.list_p_r.append (' ping error ', SELF.STR_IP)



def cmp_s (TOUPE_STR):

Str_val = toupe_str[1]

Ret_group = Re.match ("\d*", Str_val[::-1])

Str_ret = Ret_group.group ()

return int (str_ret[::-1])



thread_id = []

For I in Range (Ip_num):

Thread_id.append (0)

Thread_id[i] = Pingthread ("198.9.6.%d"% i, int (I/20), List_ping_result)

Thread_id[i].start ()

Print (I, end= ")


While True:

If Len (list_ping_result) >= Ip_num:

List_ping_result.sort (key=cmp_s)

For I in List_ping_result:

Print (i)


Break

The results are as follows:

Can be verified by a flat command:

Tested using 28 and 29IP:


Python script implementation of corporate office network IP paragraph usage

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.