-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
112 lines (80 loc) · 2.75 KB
/
util.py
File metadata and controls
112 lines (80 loc) · 2.75 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
''' Utility functions '''
import sys
import os
import json
import urllib.request
import wmi
# import pypiwin32
def get_config():
''' Get config from file if available otherwise invoke user input '''
try:
with open(os.path.join(os.getcwd(), 'config.json'), 'r') as json_data:
return json.load(json_data)
except FileNotFoundError:
print('\nNo valid config found, please provide the required information. \n\n')
sys.exit(0)
def get_nic_input(configs):
''' Get NIC input from user and prints the name of the same'''
if len(configs) == 1:
selected_nic = 0
else:
selected_nic = None
print('\nPlease select an NIC adapter: \n')
while selected_nic is None:
try:
selected_nic = int(input()) - 1
except ValueError:
print(
'Invalid input, please enter an integer between 1-{0}'.format(len(configs)))
selected_nic = None
if selected_nic < 0 or selected_nic > len(configs):
print(
'Invalid input, please enter an integer between 1-{0}'.format(len(configs)))
selected_nic = None
print_nic_name(configs[selected_nic])
return selected_nic
def print_nic_list(configs):
''' Prints NIC names from the config list '''
for index, config in enumerate(configs):
name = config.Description
print('{0}: {1}'.format(index + 1, name))
def print_nic_name(config):
''' Prints NIC name from the config '''
name = config.Description
print('\nTarget NIC: {0}\n'.format(name))
def get_nic_name(config):
''' Returns NIC name from the config '''
name = config.Description
return name
def get_nic_list():
''' Obtain network adaptors configurations '''
configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True)
return configs
def is_connected():
''' Check if internet connection is working '''
try:
with urllib.request.urlopen('https://duckduckgo.com') as req:
return req.status == 200
except:
return False
def prepare_ip_range(start_ip, end_ip):
''' Prepare list based on the provided range '''
start = list(map(int, start_ip.split(".")))
end = list(map(int, end_ip.split(".")))
temp = start
ip_range = []
ip_range.append(start_ip)
while temp != end:
temp[3] += 1
for i in (3, 2, 1):
if temp[i] == 256:
temp[i] = 0
temp[i - 1] += 1
ip_range.append(".".join(map(str, temp)))
return ip_range
def reset_nic(index):
''' Reset network adapter to use DHCP'''
nic_configs = get_nic_list()
nic = nic_configs[index]
# Enable DHCP
nic.EnableDHCP()