前言
ip段分配给项目之后,不清楚项目组已经使用了那些IP,用一个脚本检查当前在线IP
脚本
# -*- coding: utf-8 -*-
"""
Created on 2021-11-07 21:16:39
---------
@summary:
检查IP段存活IP
---------
@author: wxzy
"""
import os
import sys
import argparse
import ipaddress
from ping3 import ping
#定义变量
PROJECT = {
'项目一': [
'49.119.110.0/24',
],
'项目三': [
'60.13.119.176/28',
],
'项目四': [
'60.190.118.128/25',
],
'项目五': [
'61.128.125.0/25',
'61.138.119.160/28',
'61.234.118.0/25',
],
}
def ip_ping(ip_addr):
"""
检查IP是否存活
"""
os_code = ping(str(ip_addr),timeout=0.5)
return os_code
def ping_check_alive(datadict):
"""
检查存活ip
"""
for name, nets in datadict.items():
for net in nets:
i = 1
ip_net = ipaddress.ip_network(net, strict=False)
for ip in ip_net.hosts():
if ip_ping(ip):
print('{}-{}-{},{};'.format(name, net, ip, ip))
i += 1
#if i > 1:
# break
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="检查网段存活IP")
parser.add_argument("-c", "--check", action="store_true", help="检查项目存活IP")
if not sys.argv[1:]:
sys.argv.extend(['-h'])
args = parser.parse_args()
if args.check:
ping_check_alive(PROJECT)
exit()
执行
python3 check_alive.py
评论 (0)