-
Notifications
You must be signed in to change notification settings - Fork 0
/
free_ip_finder.py
66 lines (55 loc) · 2.37 KB
/
free_ip_finder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import subprocess
import ipaddress
import concurrent.futures
def ping_ip(ip):
result = subprocess.call(['ping', '-c', '1', '-W', '0.2', str(ip)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return ip if result != 0 else None
def find_free_ip_management_subnet(cnt):
free_ips = []
start_ip = ipaddress.ip_address('10.196.149.52')
end_ip = ipaddress.ip_address('10.196.149.126')
ips_to_ping = [str(ip) for ip in range(int(start_ip), int(end_ip) + 1)]
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = {executor.submit(ping_ip, str(ipaddress.ip_address(ip))): ip for ip in range(int(start_ip), int(end_ip) + 1)}
for future in concurrent.futures.as_completed(futures):
ip = futures[future]
if future.result() is not None:
free_ips.append(ipaddress.ip_address(ip))
if len(free_ips) >= int(cnt):
break
return free_ips
def find_free_ip_data_vlans(vlan, cnt):
free_ips = []
start_ip = ipaddress.ip_address(f'192.168.{vlan}.0')
end_ip = ipaddress.ip_address(f'192.168.{vlan}.254')
ips_to_ping = [str(ip) for ip in range(int(start_ip), int(end_ip) + 1)]
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = {executor.submit(ping_ip, str(ipaddress.ip_address(ip))): ip for ip in range(int(start_ip), int(end_ip) + 1)}
for future in concurrent.futures.as_completed(futures):
ip = futures[future]
if future.result() is not None:
free_ips.append(ipaddress.ip_address(ip))
if len(free_ips) >= int(cnt):
break
return free_ips
def main():
print("Options:")
print("1) Find free IP in management subnet")
print("2) Find free IP from data VLANs")
option = input("Enter your choice: ").strip().lower()
ip_cnt = input("Number of IPs needed: ").strip().lower()
if option == '1':
free_ips = find_free_ip_management_subnet(ip_cnt)
print("Free IPs in management subnet:")
for ip in free_ips:
print(ip)
elif option == '2':
vlan = input("Enter VLAN ID: ").strip()
free_ips = find_free_ip_data_vlans(vlan, ip_cnt)
print("Free IPs in data VLAN:")
for ip in free_ips:
print(ip)
else:
print("Invalid option")
if __name__ == "__main__":
main()