cool77
cool77

Reputation: 1136

python script to replace third byte of IP address

I have a python script which needs to run an operation by modifying an existing IP address. it needs to replace the third byte of an IP address which is constant in all cases for me. x.x.16.x to x.x.17.x

i can use a str.replace('16','17') if i know that there wont be any '16' in any other byte.

but i am trying to make it more generic. is there a simpler way to achieve this without using a lot of parsing.

Upvotes: 0

Views: 122

Answers (1)

cymruu
cymruu

Reputation: 3008

Maybe something like this

ips = ["x.16.x.x", "16.x.x.x"]

for ip in ips:
  p = ip.split(".")
  p[1] = "16"
  print(".".join(p))

Upvotes: 1

Related Questions