当前位置:网站首页>IP地址转换地址段
IP地址转换地址段
2022-07-02 14:08:00 【明明如月001】
近期工作需要,写了一个地址转化的小脚本,方便找到对应IP归属地址段,对于运维人员有一定辅助作用
中间有些地方可以简化,不过也懒得改了…
如下:
# coding:utf-8
def transition_to_sub26(file):
with open(file, 'r', encoding='utf-8') as file:
for ip in file:
ip_end = ip.split(".")[3]
ip_end = int(ip_end)
def sub_net(ip_end):
if ip_end < 64:
return "0/26"
elif ip_end < 128:
return "64/26"
elif ip_end < 192:
return "128/26"
else:
return "192/26"
res = sub_net(ip_end)
# print(res)
ip_join = ".".join(ip.split(".")[0:3])
# print(ip_join) #10.41.0
ip_sub = ip_join + "." + res
# return ip_sub
print(ip_sub) # 10.41.0.64/26
with open("IP地址转换结果(26位).txt", 'a+', encoding='utf-8') as file1:
file1.write(ip_sub + "\n")
def transition_to_sub28(file):
with open(file, 'r', encoding='utf-8') as file:
for ip in file:
ip_end = ip.split(".")[3]
ip_end = int(ip_end)
def sub_net(ip_end):
if ip_end < 16:
return "0/28"
elif ip_end < 32:
return "16/28"
elif ip_end < 48:
return "32/28"
elif ip_end < 64:
return "48/28"
elif ip_end < 80:
return "64/28"
elif ip_end < 96:
return "80/28"
elif ip_end < 112:
return "96/28"
elif ip_end < 128:
return "112/28"
elif ip_end < 144:
return "128/28"
elif ip_end < 160:
return "144/28"
elif ip_end < 176:
return "160/28"
elif ip_end < 192:
return "176/28"
elif ip_end < 208:
return "192/28"
elif ip_end < 224:
return "208/28"
elif ip_end < 240:
return "224/28"
else:
return "240/28"
res = sub_net(ip_end)
# print(res)
ip_join = ".".join(ip.split(".")[0:3])
# print(ip_join) #10.41.0
ip_sub = ip_join + "." + res
print(ip_sub) # 10.41.0.64/26
with open("IP地址转换结果(28位).txt", 'a+', encoding='utf-8') as file2:
file2.write(ip_sub + "\n")
if __name__ == '__main__':
file = input("请输入IP地址文件路径:" + "\n")
input_result = input("请输入需要转换的地址段,【1】代表转化为26位,【2】代表转化为28位:" + "\n")
if input_result == "1":
transition_to_sub26(file)
if input_result == "2":
transition_to_sub28(file)
边栏推荐
- What is the difference between self attention mechanism and fully connected graph convolution network (GCN)?
- Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
- john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
- PCL least median square method fitting plane
- AcWing 300. Task arrangement
- Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
- Take you ten days to easily complete the go micro service series (I)
- vscode设置删除行快捷键[通俗易懂]
- Exploration and practice of integration of streaming and wholesale in jd.com
- Serial port controls steering gear rotation
猜你喜欢

Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!

Routing mode: hash and history mode

TCP拥塞控制详解 | 2. 背景

Easy language ABCD sort

电脑自带软件使图片底色变为透明(抠图白底)

Data security industry series Salon (III) | data security industry standard system construction theme Salon

Notice on holding a salon for young editors of scientific and Technological Journals -- the abilities and promotion strategies that young editors should have in the new era

流批一体在京东的探索与实践

串口控制舵机转动

PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
随机推荐
vscode设置删除行快捷键[通俗易懂]
配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
Yyds dry goods inventory has not revealed the artifact? Valentine's Day is coming. Please send her a special gift~
&lt;四&gt; H264解码输出yuv文件
PWM controlled steering gear
Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
How to solve the failure of printer driver installation of computer equipment
什么是泛型?- 泛型入门篇
Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
DigiCert SSL证书支持中文域名申请吗?
sql解决连续登录问题变形-节假日过滤
A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
Which software is good for machine vision?
pwm呼吸灯
[cloud native] briefly talk about the understanding of flume, a massive data collection component
Yolov5 practice: teach object detection by hand
Cloud native cicd framework: Tekton
你想要的宏基因组-微生物组知识全在这(2022.7)
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)