温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python脚本实现公司办公网ip段落使用情况

发布时间:2020-07-25 23:40:42 来源:网络 阅读:4939 作者:tty之星 栏目:编程语言

由于公司使用的是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

结果如下:

python脚本实现公司办公网ip段落使用情况

可以通过平命令进行验证:

使用上图的28和29ip进行测试:

python脚本实现公司办公网ip段落使用情况


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI