当前位置:网站首页>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)
边栏推荐
- C语言自定义函数的方法
- Digital IC hand tearing code -- voting device
- According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
- What is normal distribution? What is the 28 law?
- PCL 最小中值平方法拟合平面
- Download blender on Alibaba cloud image station
- P6774 [noi2020] tears in the era (block)
- Global and Chinese markets for airport baggage claim conveyors 2022-2028: technology, participants, trends, market size and share Research Report
- 深度学习图像数据自动标注[通俗易懂]
- Exploration and practice of integration of streaming and wholesale in jd.com
猜你喜欢

PWM controlled steering gear

PWM控制舵机

La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter

PWM breathing lamp

Digital IC hand tearing code -- voting device

Hard core! One configuration center for 8 classes!

LeetCode 2. 两数相加

How openharmony starts FA of remote devices

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

七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
随机推荐
Learning Weekly - total issue 60 - 25th week of 2022
John blasting appears using default input encoding: UTF-8 loaded 1 password hash (bcrypt [blowfish 32/64 x3])
Xiaopeng P7 had an accident on rainy days, and the airbag did not pop up. Official response: the impact strength did not meet the ejection requirements
pwm呼吸燈
Serial port controls steering gear rotation
基于Impala的高性能数仓实践之执行引擎模块
Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
TCP congestion control details | 2 background
How to solve the failure of printer driver installation of computer equipment
LeetCode 1. 两数之和
LeetCode 5. Longest Palindromic Substring
Exploration and practice of integration of streaming and wholesale in jd.com
uboot的作用和功能
ROW_NUMBER()、RANK()、DENSE_RANK区别
国内比较好的OJ平台[通俗易懂]
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
Analysis of how to prevent virus in industrial computer
Take you ten days to easily complete the go micro service series (I)
Detailed explanation of @accessories annotation of Lombok plug-in
618 deep resumption: Haier Zhijia's winning methodology