python指令碼實現公司辦公網ip段落使用方式

來源:互聯網
上載者:User

標籤:thread   water   tle   image   驗證   sel   def   break   images   

由於公司使用的是VMware虛擬化技術,每次建立虛機都要分配一個串連外網的ip地址,另外公司規模擴大,新人也需要分配ip地址,那麼很容易造成ip地址的爛用和ip地址的衝突,

所以決定通過一個指令碼進行ip的空閑使用方式進行排查,減少營運的時間成本,較少ip衝突的情況:

指令碼內容如下:

如果要使用可以修改成自己公司的ip地址即可:


# /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

結果如下:

可以通過平命令進行驗證:

使用的28和29ip進行測試:


python指令碼實現公司辦公網ip段落使用方式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.