Increasing Python ip Address

Asked 2 years ago, Updated 2 years ago, 21 views

If I want to specify ip = '192.168.0.1' variable and increase it sequentially like 192.168.0.2, 192.168.0.3, what should I do?

python

2022-09-22 22:56

2 Answers

There are many ways.

for i in range(1, 100):
    ip = f'192.168.0.{i}'
    print(ip)

a = [1, 2, 3, 4, 55, 6, 7]
for i in a:
    ip = f'192.168.0.{i}'
    print(ip)
ip = '192.168.0.1'
list_ip = ip.split('.')
for i in range(int(ip[-1]), 100):
    list_ip[-1] = str(i)
    ip = '.'.join(list_ip)
    print(ip)


2022-09-22 22:56

I think it would be good to make the place of the ip into each variable.

ip = [192, 168, 0, 1]

for i in range(10):
    ip[-1] += 1
    print(".".join(map(str, ip)))


2022-09-22 22:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.