Sunday, April 20, 2025

Split an IP Address into Four Parts - Python

ip = '192.168.5.5'

parts = ip.split('.')

part_0 = parts[0]
part_1 = parts[1]
part_2 = parts[2]
part_3 = parts[3]

print("-" * 60)

print('Part: 0', part_0)
print('Part: 1', part_1)
print('Part: 2', part_2)
print('Part: 3', part_3)

print("-" * 60)

sep = '.' 

for x in range(1, 256):
	print(part_0 + sep + part_1 + sep + part_2 + sep + part_3 + sep + str(x))

This code takes an IP address (in this case, 192.168.5.5) and splits it into its four parts (part_0, part_1, part_2, and part_3) using the "." character as a delimiter. It then prints each part on a separate line.

After that, it enters a loop that generates a list of IP addresses that have the same first three parts as the original IP address (192.168.5), but with the fourth part ranging from 1 to 255. These IP addresses are printed to the console, each on a separate line, using the same separator "." as before.

This code generates a list of IP addresses that can be used to scan a local network for connected devices.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...