当前位置:网站首页>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)
边栏推荐
- 学习周刊-总第60期-2022年第25周
- 你想要的宏基因组-微生物组知识全在这(2022.7)
- Thinking about absolute truth and relative truth
- 618深度複盤:海爾智家的制勝方法論
- 配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
- Aike AI frontier promotion (2.15)
- 2322. 从树中删除边的最小分数(异或和&模拟)
- &lt;四&gt; H264解码输出yuv文件
- Unity使用UGUI设置一个简单多级水平方向下拉菜单(不需要代码)
- Executive engine module of high performance data warehouse practice based on Impala
猜你喜欢
Take you ten days to easily complete the go micro service series (I)
Kubernetes three open interfaces first sight
Exploration and practice of integration of streaming and wholesale in jd.com
July 1st gift: Yi Jingjie's "hundred day battle" ended perfectly, and the database of Guiyang bank was sealed in advance
七一献礼:易鲸捷 “百日会战”完美收官 贵阳银行数据库提前封板
LeetCode 2. 两数相加
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
PWM controlled steering gear
SQL solves the problem of continuous login deformation holiday filtering
Serial port controls steering gear rotation
随机推荐
unity Hub 登录框变得很窄 无法登录
PCL 最小中值平方法拟合平面
What if the win11 app store cannot load the page? Win11 store cannot load page
Detailed explanation of @accessories annotation of Lombok plug-in
亚马逊云科技 Community Builder 申请窗口开启
国内比较好的OJ平台[通俗易懂]
C语言中sprintf()函数的用法
618 deep resumption: Haier Zhijia's winning methodology
数字IC手撕代码--投票表决器
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
机器学习-感知机模型
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
[cloud native] briefly talk about the understanding of flume, a massive data collection component
LeetCode 2. 两数相加
Which software is good for machine vision?
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
基于多元时间序列对高考预测分析案例
Global and Chinese market of jacquard looms 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode 5. 最长回文子串
618深度複盤:海爾智家的制勝方法論