-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUbuntuNetworkConfig.py
More file actions
144 lines (119 loc) · 4.03 KB
/
UbuntuNetworkConfig.py
File metadata and controls
144 lines (119 loc) · 4.03 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
142
143
144
#!/usr/bin/env python3
import os
import subprocess
import yaml
import glob
def get_netplan_files():
"""
Detects and returns a list of Netplan configuration files.
Returns:
list: A list of Netplan configuration file paths.
"""
netplan_dir = '/etc/netplan/'
netplan_files = glob.glob(netplan_dir + '*')
return netplan_files
def select_netplan_file(netplan_files):
"""
Presents a list of detected Netplan files to the user and allows them to select one.
Args:
netplan_files: A list of Netplan configuration file paths.
Returns:
str: The path of the selected Netplan file.
"""
if not netplan_files:
print("No Netplan configuration files found.")
return None
print("Detected Netplan files:")
for i, file in enumerate(netplan_files):
print(f"{i+1}. {file}")
while True:
try:
choice = int(input("Select the Netplan file to modify (enter the number): "))
if 1 <= choice <= len(netplan_files):
return netplan_files[choice - 1]
else:
print("Invalid choice. Please select a number from the list.")
except ValueError:
print("Invalid input. Please enter a number.")
def get_user_input():
"""
Prompts the user for network configuration details.
Returns:
tuple: A tuple containing the network adapter name, IP address, subnet mask, gateway, and DNS servers.
"""
adapter = input("Enter network adapter name (e.g., ens18): ")
ip = input("Enter IP address (e.g., 192.168.1.100): ")
subnet = input("Enter subnet mask (e.g., 24): ")
gateway = input("Enter gateway IP address (e.g., 192.168.1.1): ")
dns_servers = input("Enter DNS server addresses (comma-separated, e.g., 8.8.8.8,1.1.1.1): ")
return adapter, ip, subnet, gateway, dns_servers
def generate_netplan_config(adapter, ip, subnet, gateway, dns_servers):
"""
Generates the Netplan YAML configuration based on user input.
Args:
adapter: Network adapter name.
ip: IP address.
subnet: Subnet mask.
gateway: Gateway IP address.
dns_servers: Comma-separated list of DNS server addresses.
Returns:
str: The generated YAML configuration.
"""
dns_server_list = dns_servers.split(',')
config = f"""
network:
version: 2
renderer: networkd
ethernets:
{adapter}:
addresses: [{ip}/{subnet}]
gateway4: {gateway}
nameservers:
addresses: {dns_server_list}
"""
return config
def save_netplan_config(config, filename):
"""
Saves the generated YAML configuration to the specified Netplan file.
Args:
config: The YAML configuration string.
filename: The path to the Netplan file.
"""
with open(filename, 'w') as f:
f.write(config)
def confirm_action(message):
"""
Asks the user to confirm an action.
Args:
message: The message to display to the user.
Returns:
bool: True if the user confirms, False otherwise.
"""
while True:
response = input(f"{message} (yes/no): ").lower()
if response in ['yes', 'y']:
return True
elif response in ['no', 'n']:
return False
else:
print("Invalid input. Please enter 'yes' or 'no'.")
def apply_netplan_config():
"""
Applies the Netplan configuration.
"""
subprocess.run(['sudo', 'netplan', 'apply'])
def main():
"""
Main function to execute the script.
"""
netplan_files = get_netplan_files()
selected_file = select_netplan_file(netplan_files)
if selected_file:
adapter, ip, subnet, gateway, dns_servers = get_user_input()
config = generate_netplan_config(adapter, ip, subnet, gateway, dns_servers)
if confirm_action(f"Write configuration to {selected_file}? "):
save_netplan_config(config, selected_file)
if confirm_action("Apply Netplan configuration?"):
apply_netplan_config()
if __name__ == "__main__":
main()