Tuesday, July 30, 2019

List all possible IPs in a Subnetwork

 Script

#!/usr/bin/env python

# python cidr.py <Network-address>/<Subnet mask in short notation>


import sys, struct, socket


(ip, cidr) = sys.argv[1].split('/')

cidr = int(cidr)

host_bits = 32 - cidr

i = struct.unpack('>I', socket.inet_aton(ip))[0] # note the lastianness

first = (i >> host_bits) << host_bits # clear the host bits

last = first | ((1 << host_bits) - 1)


# excludes the first and last address in the subnet

for i in range(first, last):

    print(socket.inet_ntoa(struct.pack('>I',i)))


Execution

python3 ip-list-all-IPs-in-the-subnetwork.py 10.133.72.0/28 | tail -n +2 | cat -n | awk '{print $1 ": " $2}'

1: 10.133.72.1

2: 10.133.72.2

3: 10.133.72.3

4: 10.133.72.4

5: 10.133.72.5

6: 10.133.72.6

7: 10.133.72.7

8: 10.133.72.8

9: 10.133.72.9

10: 10.133.72.10

11: 10.133.72.11

12: 10.133.72.12

13: 10.133.72.13

14: 10.133.72.14



python3 ip-list-all-IPs-in-the-subnetwork.py 10.133.72.0/27 | tail -n +2 | cat -n | awk '{print $1 ": " $2}'

1: 10.133.72.1

2: 10.133.72.2

3: 10.133.72.3

4: 10.133.72.4

5: 10.133.72.5

6: 10.133.72.6

7: 10.133.72.7

8: 10.133.72.8

9: 10.133.72.9

10: 10.133.72.10

11: 10.133.72.11

12: 10.133.72.12

13: 10.133.72.13

14: 10.133.72.14

15: 10.133.72.15

16: 10.133.72.16

17: 10.133.72.17

18: 10.133.72.18

19: 10.133.72.19

20: 10.133.72.20

21: 10.133.72.21

22: 10.133.72.22

23: 10.133.72.23

24: 10.133.72.24

25: 10.133.72.25

26: 10.133.72.26

27: 10.133.72.27

28: 10.133.72.28

29: 10.133.72.29

30: 10.133.72.30












No comments:

Post a Comment