Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
END_CONFIG=/etc/netplan/01-network-card.yaml
generateAndApply() {
sudo netplan generate
sudo netplan apply
}
getInternetInfo() {
local INTERNET_INFO=$( ip r | grep default )
printf "%s" "$( echo $INTERNET_INFO | cut -f$1 -d' ' )"
}
#static information
NAMESERVERS=("1.1.1.1" "1.0.0.1")
NETWORK_MANAGER="NetworkManager"
# information that varies
IP="$( ip r | grep kernel | cut -f9 -d' ' )"
GATEWAY="$( getInternetInfo 3 )"
DEVICE_NAME="$( getInternetInfo 5 )"
METHOD="$( getInternetInfo 7 )"
PREFIX="$( ip r | grep kernel | cut -f1 -d' ' | cut -f2 -d'/' )"
createStaticYAML() {
local YAML="network:\n"
YAML+=" version: 2\n"
YAML+=" renderer: $NETWORK_MANAGER\n"
YAML+=" ethernets:\n"
YAML+=" $DEVICE_NAME:\n"
YAML+=" dhcp4: no\n"
YAML+=" addresses: [$IP/$PREFIX]\n"
YAML+=" gateway4: $GATEWAY\n"
YAML+=" nameservers:\n"
YAML+=" addresses: [${NAMESERVERS[0]},${NAMESERVERS[1]}]"
printf "%s" "$YAML"
}
clearConfigs() {
[ -f $END_CONFIG ] && sudo rm $END_CONFIG
}
setYAML() {
sudo echo -e "$(createStaticYAML)" > $END_CONFIG
}
clearConfigs
setYAML
generateAndApply
restartNetwork
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment