Upload configure netplan script.

This commit is contained in:
BoHung Chiu 2022-08-25 14:21:42 +08:00
parent 2d2cfb805f
commit 38ca9a4ae0
3 changed files with 161 additions and 0 deletions

142
configure_netplan.py Normal file
View File

@ -0,0 +1,142 @@
#!/usr/bin/python3
import sys
import os
import yaml
import subprocess
import ipaddress
import re
print(123)
def main():
args = sys.argv
if len(args) < 2:
print(f"Usage: {args[0]} /etc/netplan/00-installer-config.yaml")
sys.exit()
netplan_config_file = args[1]
with open(netplan_config_file, 'r') as f:
org_file_contents = f.read()
netplan_config_yaml = yaml.safe_load(org_file_contents)
if netplan_config_yaml == None:
netplan_config_yaml = {}
addresses = {}
gateway4 = {}
dhcp = {}
network_block = netplan_config_yaml.get('network')
no_network_block = False
no_ethernets_block = False
if network_block == None:
no_network_block = True
no_ethernets_block = True
else:
ethernets_block = network_block.get('ethernets')
if ethernets_block == None:
no_ethernets_block = True
else:
for interface in ethernets_block:
interface_block = ethernets_block[interface]
interface_addresses = interface_block.get('addresses')
if interface_addresses != None:
if len(interface_addresses) != 0:
addresses[interface] = interface_addresses
if len(addresses) != 0:
print("You alreay configure some addresses for interfaces!")
for interface in addresses:
interface_addresses = addresses[interface]
for addr in interface_addresses:
print(f"{interface}: {addr}")
ans = input("Do you want to continue configure? (y/n)")
if ans.lower() != "y":
sys.exit()
default_interfaces = list(set(subprocess.Popen("ip -o -4 route show to default | awk '{print $5}'", shell=True, stdout=subprocess.PIPE).stdout.read().decode().strip().split("\n")))
if len(default_interfaces) == 0:
if len(args) > 2:
default_interfaces = args[2]
else:
print("No network interface detected!")
print(f"Usage: {args[0]} /etc/netplan/00-installer-config.yaml $interface_name")
sys.exit()
change_flag = False
for interface in default_interfaces:
old_address = addresses.get(interface)
if old_address != None:
for addr in old_address:
print(f"Found {addr} for interface {interface}!")
ans = input("Do you want to continue configure? (y/n)")
if ans.lower() != "y":
continue
new_addr = ""
dhcp4 = input(f"Do you want to use dynamic address(dhcp) for interface {interface}? (y/n)")
if dhcp4.lower() != "y":
dhcp4 = "no"
while new_addr == "":
new_addr = input(f"New address for interface {interface}: ")
try:
ipaddress.IPv4Address(new_addr)
except:
print("Invalid ipaddress!")
new_addr = ""
addresses[interface] = [new_addr + '/24']
gateway4[interface] = re.sub('[\\d]+$', '254', new_addr)
change_flag = True
else:
change_flag = True
dhcp4 = "true"
if old_address != None:
del addresses[interface]
dhcp[interface] = dhcp4
if no_network_block:
netplan_config_yaml['network'] = {}
change_flag = True
if no_ethernets_block:
change_flag = True
netplan_config_yaml['network']['ethernets'] = {}
if netplan_config_yaml['network'].get('version') == None:
netplan_config_yaml['network']['version'] = 2
if change_flag:
for interface in default_interfaces:
interface_content = netplan_config_yaml['network']['ethernets'].get(interface)
if interface_content == None:
interface_content = {}
netplan_config_yaml['network']['ethernets'][interface] = interface_content
new_addr = addresses.get(interface)
if new_addr != None:
if type(new_addr) != list:
new_addr = [new_addr]
netplan_config_yaml['network']['ethernets'][interface]['addresses'] = new_addr
else:
tmp = netplan_config_yaml['network']['ethernets'][interface]
if tmp.get('addresses') != None:
del netplan_config_yaml['network']['ethernets'][interface]['addresses']
if tmp.get('nameservers') != None:
del netplan_config_yaml['network']['ethernets'][interface]['nameservers']
if tmp.get('gateway4') != None:
del netplan_config_yaml['network']['ethernets'][interface]['gateway4']
new_gateway4 = gateway4.get(interface)
if new_gateway4 != None:
netplan_config_yaml['network']['ethernets'][interface]['gateway4'] = new_gateway4
dhcp4 = dhcp.get(interface)
if dhcp4 != None:
netplan_config_yaml['network']['ethernets'][interface]['dhcp4'] = dhcp4
nameservers = interface_content.get('nameservers')
if nameservers == None and dhcp4 == 'no':
nameservers = {'addresses': []}
if nameservers != None:
nameservers_addresses = nameservers.get('addresses')
if nameservers_addresses != None:
default_nameservers = ['8.8.8.8', '8.8.4.4']
for nameserver in default_nameservers:
if nameserver not in nameservers_addresses:
nameservers_addresses.append(nameserver)
nameservers['addresses'] = nameservers_addresses
netplan_config_yaml['network']['ethernets'][interface]['nameservers'] = nameservers
with open(netplan_config_file, 'w+') as f:
yaml.dump(netplan_config_yaml, f)
netplan_get_status = os.system('sudo netplan get')
if netplan_get_status == 0:
os.system('sudo netplan try --timeout 10')
print("Finish configuring netplan!")
else:
print("Something went wrong!")
f.write(org_file_contents)
print("Recover changed!")
if __name__ == '__main__':
main()

16
configure_netplan.sh Normal file
View File

@ -0,0 +1,16 @@
#!/usr/bin/bash
if [[ -z "$(which netplan)" ]]; then
echo "Your system is not support netplan!"
echo "Please generate your network config!"
else
netplan_configs_count=`ls /etc/netplan/*.yaml|wc -l`
netplan_config_file=""
if [[ "$netplan_configs_count" == "0" ]]; then
netplan_config_file="/etc/netplan/00-installer-config.yaml"
sudo wget http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/default_netplan-config.yaml -O $netplan_config_file
else
netplan_config_file=`ls /etc/netplan/*.yaml|xargs|awk '{print $1}'`
fi
wget http://gitlab.tp.rulingcom.com/erictyl/install_r45_on_ubuntu_1804lts_doc/-/raw/master/configure_netplan.py -O configure_netplan.py
sudo python3 configure_netplan.py "$netplan_config_file"
fi

View File

@ -0,0 +1,3 @@
# This is the network config written by 'subiquity'
network:
version: 2