简体   繁体   English

Python3添加IP地址的最后一个八位位组

[英]Python3 adding the last octet of an IP address

I'm parsing a file and one of the columns is the subnet for example 192.168.0.0/24. 我正在解析一个文件,其中一列是子网,例如192.168.0.0/24。 I would like to take that subnet and just add 3 to the last octet and with the end result of 192.168.0.3 without the /24. 我想使用该子网,仅将3添加到最后一个八位位组,最终结果为192.168.0.3,不带/ 24。 I was able to open the file for reading, split it into ',' locate the subnet and add but I can't produce 192.168.0.3 I only produce 3. 我能够打开文件进行读取,将其拆分为“,”找到子网并添加,但我无法生成192.168.0.3,我只能生成3。

import re

file=open('/home/lab01/clients/a/2018/automation/Production_VRF','r')
for items in file:
 _gw=(items.split(',')[2])
 Var6=re.split(r'[./]',_gw)
 Var7=(Var6[3])
 vip=sum=int(Var7)+3
 print(vip)

Thank you! 谢谢!

您应该在Var6中添加前三个元素的串联和已指定的值:

ip = Var6[0]+"."+Var6[1]+"."+Var6[2]+"."+str(vip)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将最后一个八位字节与 IP 地址隔离,并将其放入变量中 - Isolate the last octet from an IP address and put it in a variable 正则表达式在 Google 表格中提取 IP 地址(第 3 和第 4 八位字节)的最后两个八位字节 - Regex to extract last two octets of IP address (3rd and 4th Octet) in Google Sheets 匹配IP地址但最后一个八位位组在特定范围内的动态AngularJS验证 - Dynamic AngularJS validation that matches an IP address, but where the last octet is within a specific range 从IP地址提取前三个八位字节 - Extract first three octet from IP address 具有8个或更多零的八位字节的IP地址是否为有效的IP地址? - Is an IP address with an octet given by 4 or more zeros a valid IP address? 通过python regex忽略IP地址的第一个八位字节中的这两个数字(以127或0开头) - Ignore these two numbers (starts with 127 or 0) in the first octet of the ip address through python regex python3:从编译模式中提取 IP 地址 - python3: extract IP address from compiled pattern 使用正则表达式拆分从IP地址中删除第一个八位位组 - Remove first octet from IP address with Regex split 获取最后一个网段的IP地址 - Get last segment IP address 删除IP地址的最后一个八位位组 - Deleting the last octets of an IP address
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM