-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_list.py
54 lines (51 loc) · 2.12 KB
/
ip_list.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
import re
def valid_ip(ip):
'''Function to validate IPv4 format in IP list using regex
'''
if re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip):
return ip
else:
print('wrong ip')
def valid_port(port):
'''Function to validate SSH port range
'''
if port.isdecimal():
return port
else:
print("wrong ssh port format:", port)
#Get a list of IPs to run the main ip session function on
#e.g [['192.168.254.78', 'root', 'admin'], '192.168.254.86']
ip_list = []
with open('../_main/files/ip_list.yml') as f: #/home/labuser/DEV/co_op/_main/
try:
for line in f.readlines():
#print(line.rstrip().partition('#')[0])
print(len(line.partition('#')[0].rstrip().split(',')))
if line[0].isdecimal() == False:
#Skip lines not starting with a number
pass
elif len(line.partition('#')[0].rstrip().split(',')) == 1:
#Check to see if only IP is provided, skip comments at the end of line
ip = line.partition('#')[0].rstrip()
if valid_ip(ip):
ip_list.append(ip)
elif len(line.partition('#')[0].rstrip().split(',')) == 3:
#IP, username, password and optional port number provided
ip = line.partition('#')[0].rstrip().split(',')
if valid_ip(ip[0]):
ip_list.append(ip)
elif len(line.partition('#')[0].rstrip().split(',')) == 4:
ip = line.partition('#')[0].rstrip().split(',')
if valid_ip(ip[0]):
if ip[3].isnumeric():
ip_list.append(ip)
else:
print(f"Not a valid port definition: {ip[3]} in {ip}")
elif len(line.partition('#')[0].rstrip().split(',')) > 4:
ip = line.partition('#')[0].rstrip().split(',')
print(f"Not a valid definition, check ambiguous ',' in the line: {ip}")
except FileNotFoundError as err:
print("Missing IP list file!")
except Exception as ex:
print(ex)
print(ip_list)