2

Sorry i am new to python so I would like to inquire something. Let's say the user enter his IP ( 192.168.1.10 ), i want to start looping from his input (192.168.1.10) to the end of the sub net 192.168.1.255/24.

I was thinking something along this line

for i in range(256):
    IP = "192.168.1.%d" (i)
    print IP

But how do i put his input inside? Any guidance will be helpful, thanks.

Cheers, Tech Newbie.

0

3 Answers 3

1

This should work for you, though I haven't tested it.

ip=raw_input("Enter an ip address:")
partToFetch= int(ip.split(".")[3])
for i in range(partToFetch,256):
    print "192.168.1.{0}".format(str(i))
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming that you take the initial ip address with something like

IP = raw_input('Enter your IP:')

You can rework your loop

for i in range(256):
    print(IP[:IP.rfind('.')] + '.' + i)

Comments

0

Use the ipaddress module in Python 3.3. You can use an index into a subnet as a list of addresses:

>>> import ipaddress
>>> subnet = ipaddress.ip_network('192.168.1.0/24')
>>> for i in range(10, 256):
>>>     print(subnet[i])
192.168.1.10
192.168.1.11
...
192.168.1.254
192.168.1.255

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.