前提

最近剛好在幫別的企業規劃網路, 該企業對於網路切割非常的要求.

希望我切的數量剛剛好, 例如該環境只有12台機器, 就要切/28.

比起自己一直算或開網頁算, 不如自己寫一個.(其實就是連開瀏覽器都懶)

順便練習一下Python, 說寫就寫:

#!/bin/env python3

"""
Author : GordonWei
Date : 04/16/21
Comment : input CIDR, this python will output ip range
"""

import ipaddress

choice = input('''
Which output does you want to show?
1. Only show cidr start and end.
2. Show all cidr ip-address.
''')
cidr = input('Enter CIDR include slash num : ')
net = ipaddress.ip_network(cidr)
if choice == '1' :
        print('Your CIDR is ' + cidr)
        print('CIDR Start From : ' + str(net[0]))
        print('CIDR End To : ' + str(net[-1]))
elif choice == '2':
        for i in net:
                print(i)
else:
        None

使用方法:

Step 1 : 執行檔案cidr.py

python3 cidr.py

Step 2 : 根據提示輸入1或2

Which output does you want to show?
1. Only show cidr start and end.
2. Show all cidr ip-address.
1

選1的話會直接顯示開頭與結尾的IP Address

選2的話會列出所有的IP Address

Step 3 : 輸入CIDR(帶/xx)

Enter CIDR include slash num : 192.168.0.0/28

就會顯示出來了

Demo:

python3 cidr.py                                                   

Which output does you want to show?
1. Only show cidr start and end.
2. Show all cidr ip-address.
1
Enter CIDR include slash num : 192.168.0.0/28
Your CIDR is 192.168.0.0/28
CIDR Start From : 192.168.0.0
CIDR End To : 192.168.0.15
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
python3 cidr.py                                                                         

Which output does you want to show?
1. Only show cidr start and end.
2. Show all cidr ip-address.
2
Enter CIDR include slash num : 192.168.0.0/28
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
192.168.0.8
192.168.0.9
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14
192.168.0.15

這邊也放上github的連結, 想玩的可以自己下載回去另外修改增加功能

下載連結