Skip to content

Instantly share code, notes, and snippets.

@recklessop
Created December 3, 2018 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save recklessop/f79052e1c036fa2f26a218b17772f362 to your computer and use it in GitHub Desktop.
Save recklessop/f79052e1c036fa2f26a218b17772f362 to your computer and use it in GitHub Desktop.
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