Last active
September 28, 2023 08:59
-
-
Save alknopfler/395d1c7aa74adb6eb6f88495d3020b16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## Verify number of args expected: 1 | |
if [ $# -ne 1 ]; then | |
echo "Usage: $0 <network_data.json>" | |
exit 1 | |
fi | |
NETWORK_DATA_FILE=$1 | |
## Function to convert netmask to CIDR notation | |
IPprefix_by_netmask () { | |
c=0 x=0$( printf '%o' ${1//./ } ) | |
while [ $x -gt 0 ]; do | |
let c+=$((x%2)) 'x>>=1' | |
done | |
echo $c ; | |
} | |
## Verify if file Network Data exists | |
if [ ! -f $NETWORK_DATA_FILE ]; then | |
echo "File $1 not found!" | |
exit 1 | |
fi | |
## Verify if jq is installed | |
if ! command -v jq &> /dev/null | |
then | |
echo "jq could not be found!" | |
exit 1 | |
fi | |
## Verify if file Network Data is a json file | |
if ! jq -e . >/dev/null 2>&1 <<<$(cat $NETWORK_DATA_FILE); then | |
echo "File $1 is not a json file!" | |
exit 1 | |
fi | |
## Verify if file Network Data is a valid json file | |
if ! jq empty $NETWORK_DATA_FILE >/dev/null 2>&1; then | |
echo "File $1 is not a valid json file!" | |
exit 1 | |
fi | |
## Extract network data info from json file | |
MAC_ADDRESS=$(jq -r '.links[0].ethernet_mac_address' $NETWORK_DATA_FILE) | |
IFNAME=$(jq -r '.networks[0].id' $NETWORK_DATA_FILE) | |
IP_ADDRESS=$(jq -r '.networks[0].ip_address' $NETWORK_DATA_FILE) | |
NETMASK=$(jq -r '.networks[0].netmask' $NETWORK_DATA_FILE) | |
NETMASKCIDR=$(IPprefix_by_netmask $NETMASK) | |
GATEWAY=$(jq -r '.networks[0].routes[0].gateway' $NETWORK_DATA_FILE) | |
DNS=$(jq -r '.services[0].address' $NETWORK_DATA_FILE) | |
echo "MAC_ADDRESS: $MAC_ADDRESS" | |
echo "IFNAME: $IFNAME" | |
echo "IP_ADDRESS: $IP_ADDRESS" | |
echo "NETMASK: $NETMASK" | |
echo "NETMASKCIDR: $NETMASKCIDR" | |
echo "GATEWAY: $GATEWAY" | |
echo "DNS: $DNS" | |
## main function | |
# Configure network interface using nmcli distinguing if exists previously or not | |
if [ $(nmcli con show --active | tail -n +2 | awk '{print $4}') == ${IFNAME} ]; then | |
echo "Network interface ${IFNAME} already exists!" | |
echo "Configuring network interface ${IFNAME}..." | |
nmcli con mod ${IFNAME} ipv4.addresses ${IP_ADDRESS}/${NETMASKCIDR} ipv4.gateway ${GATEWAY} ipv4.dns ${DNS} ipv4.method manual | |
nmcli con mod ${IFNAME} ipv4.dns ${DNS} | |
nmcli con up ${IFNAME} | |
else | |
echo "Network interface ${IFNAME} does not exist!" | |
echo "Configuring network interface ${IFNAME}..." | |
nmcli con add type ethernet con-name ${IFNAME} ifname ${IFNAME} ip4 ${IP_ADDRESS}/${NETMASKCIDR} gw4 ${GATEWAY} ipv4.dns ${DNS} ipv4.method manual | |
nmcli con mod ${IFNAME} ipv4.dns ${DNS} | |
nmcli con up ${IFNAME} | |
fi | |
echo "Finalized network configuration!" | |
exit 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment