-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_manager.py
More file actions
141 lines (114 loc) · 5.76 KB
/
service_manager.py
File metadata and controls
141 lines (114 loc) · 5.76 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import click
import netifaces
import subprocess
@click.command()
def main():
while True:
click.echo("Choose an action:")
click.echo("1. Change network adapter")
click.echo("2. Add route to multicast address")
click.echo("3. Change hostname")
click.echo("4. Update version of 'ela' service")
click.echo("5. Check connection to Installation machine")
click.echo("Enter '0' to quit")
choice = click.prompt("Enter your choice", type=int, default=0)
if choice == 1:
change_network_adapter()
elif choice == 2:
add_route_to_multicast_address()
elif choice == 3:
change_hostname()
elif choice == 4:
update_ela_service()
elif choice == 5:
check_connection()
elif choice == 0:
break
else:
click.echo("Invalid choice. Please try again.")
if not click.confirm("Do you want to proceed with more actions?", default=True):
break
def get_network_interface_address(interface_name):
addresses = netifaces.ifaddresses(interface_name)
ipv4_info = addresses.get(netifaces.AF_INET)
if ipv4_info:
ip_address = ipv4_info[0]['addr']
subnet_mask = ipv4_info[0]['netmask']
return ip_address, subnet_mask
return None, None
def set_network_interface_address(interface_name, ip_address, subnet_mask):
subprocess.run(["sudo", "ip", "addr", "flush", "dev", interface_name])
subprocess.run(["sudo", "ip", "addr", "add", f"{ip_address}/{subnet_mask}", "dev", interface_name])
def change_network_adapter():
interfaces = netifaces.interfaces()
filtered_interfaces = [interface for interface in interfaces if any(substring in interface.lower() for substring in ["ens", "eth", "ext", "int", "eno", "dummy"])] #dummy just for tests
if not filtered_interfaces:
print("No network interfaces found.")
else:
print("Select a network interface:")
for idx, interface in enumerate(filtered_interfaces, start=1):
print(f"{idx}. {interface}")
while True:
choice = input("Enter the number of the network interface you want to select: ")
try:
choice_idx = int(choice)
if 1 <= choice_idx <= len(filtered_interfaces):
selected_interface = filtered_interfaces[choice_idx - 1]
ip_address, subnet_mask = get_network_interface_address(selected_interface)
print(f"Current IP Address: {ip_address}")
print(f"Current Subnet Mask: {subnet_mask}")
new_ip_address = input("Enter the new IP address: ")
new_subnet_mask = input("Enter the new subnet mask: ")
set_network_interface_address(selected_interface, new_ip_address, new_subnet_mask)
print("IP address and subnet mask updated successfully.")
break
else:
print("Invalid choice. Please enter a valid number.")
except ValueError:
print("Invalid input. Please enter a number.")
def add_route_to_multicast_address():
interfaces = netifaces.interfaces()
filtered_interfaces = [interface for interface in interfaces if any(substring in interface.lower() for substring in ["ens", "eth", "ext", "int", "dummy"])]
if not filtered_interfaces:
print("No network interfaces containing 'ens', 'eth', 'ext', 'int', or 'dummy' found.")
else:
print("Select a network interface:")
for idx, interface in enumerate(filtered_interfaces, start=1):
print(f"{idx}. {interface}")
while True:
choice = input("Enter the number of the network interface you want to select: ")
try:
choice_idx = int(choice)
if 1 <= choice_idx <= len(filtered_interfaces):
selected_interface = filtered_interfaces[choice_idx - 1]
# Check if the interface is up and bring it up if it's not
if not is_interface_up(selected_interface):
bring_interface_up(selected_interface)
multicast_address = input("Enter the multicast address (e.g., 224.0.0.1): ")
metric = input("Enter the metric (default is 100, if its ok press enter): ") or "100"
subnet_mask = input("Enter the subnet mask (default is 8, if its ok press enter): ") or "8"
try:
subprocess.run(["sudo", "ip", "route", "add", f"{multicast_address}/{subnet_mask}", "dev", selected_interface, "metric", metric])
print("Route to multicast address added successfully.")
except subprocess.CalledProcessError as e:
print(f"Failed to add route: {e}")
break
else:
print("Invalid choice. Please enter a valid number.")
except ValueError:
print("Invalid input. Please enter a number.")
def is_interface_up(interface_name):
# Check if the interface is up by checking its flags
flags = netifaces.ifaddresses(interface_name)
return netifaces.AF_INET in flags
def bring_interface_up(interface_name):
subprocess.run(["sudo", "ip", "link", "set", "dev", interface_name, "up"])
print(f"Interface {interface_name} is now up.")
def change_hostname():
click.echo(click.style("Changing hostname...", fg='green'))
def update_ela_service():
click.echo(click.style("Updating ela service...", fg='green'))
def check_connection():
click.echo(click.style("Checking connection to Installation machine...", fg='green'))
if __name__ == "__main__":
main()