Improvements to getStaticIPv4Settings()

- Use a regular expression to extract IPs from the 'ip' command. With this,
    there is a little need to validate output. Even though the regex will match
    invalid IPs like 192.168.23.444, 'ip' can't return them, and even if it did,
    the script would not have reached this function due to previous functions
    using the network with broken routes and addresses.

  - Get the IP address from the selected interface rather then from the 'ip route'
    command as it's not guaranteed that such IP is the same of the interface the
    user decided to use (though on a Raspberry Pi inside a home LAN, most likely
    it is, but it also maskes easier to get the IP in the CIDR notation with a
    single 'ip | grep' pipe).
This commit is contained in:
Orazio 2020-01-29 14:11:38 +01:00
parent 9679a600c1
commit 380dc0ab37

View file

@ -59,10 +59,8 @@ c=$(( columns / 2 ))
r=$(( r < 20 ? 20 : r )) r=$(( r < 20 ? 20 : r ))
c=$(( c < 70 ? 70 : c )) c=$(( c < 70 ? 70 : c ))
# Find IP (with netmask) and gateway used to route to outside world # Find the gateway IP used to route to outside world
BaseIPv4addr=$(ip route get 192.0.2.1 | awk '{print $7}') CurrentIPv4gw="$(ip -o route get 192.0.2.1 | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk 'NR==2')"
CurrentIPv4addr=$(ip -o -f inet address | grep "${BaseIPv4addr}/" | awk '{print $4}')
CurrentIPv4gw=$(ip route get 192.0.2.1 | awk '{print $3}')
# Find network interfaces whose state is UP, so as to skip virtual interfaces and the loopback interface # Find network interfaces whose state is UP, so as to skip virtual interfaces and the loopback interface
availableInterfaces=$(ip -o link | awk '/state UP/ {print $2}' | cut -d':' -f1 | cut -d'@' -f1) availableInterfaces=$(ip -o link | awk '/state UP/ {print $2}' | cut -d':' -f1 | cut -d'@' -f1)
@ -632,33 +630,11 @@ validIPAndNetmask(){
} }
getStaticIPv4Settings() { getStaticIPv4Settings() {
# Find the IP address (and netmask) of the desidered interface
CurrentIPv4addr="$(ip -o -f inet address show dev "${IPv4dev}" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2}')"
# Grab their current DNS servers # Grab their current DNS servers
CurrentIPv4dns=$(grep -v "^#" /etc/resolv.conf | grep -w nameserver | awk '{print $2}' | xargs) IPv4dns=$(grep -v "^#" /etc/resolv.conf | grep -w nameserver | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | xargs)
read -r -a CurrentIPv4dns <<< "${CurrentIPv4dns}"
IPv4dns=()
for dns in "${CurrentIPv4dns[@]}"; do
if validIP "${dns}"; then
IPv4dns+=("${dns}")
else
echo "::: Warning: invalid system DNS ${dns}"
fi
done
if [ "${#IPv4dns[@]}" -eq 0 ]; then
echo "::: Couldn't get current DNS servers from \"/etc/resolv.conf\", exiting..."
exit 1
fi
if ! validIPAndNetmask "${CurrentIPv4addr}"; then
echo "::: Couldn't get current IP address, exiting..."
exit 1
fi
if ! validIP "${CurrentIPv4gw}"; then
echo "::: Couldn't get current gateway IP, exiting..."
exit 1
fi
if [ "${runUnattended}" = 'true' ]; then if [ "${runUnattended}" = 'true' ]; then
@ -826,7 +802,7 @@ setDHCPCD(){
echo "interface ${IPv4dev} echo "interface ${IPv4dev}
static ip_address=${IPv4addr} static ip_address=${IPv4addr}
static routers=${IPv4gw} static routers=${IPv4gw}
static domain_name_servers=${IPv4dns[*]}" | $SUDO tee -a ${dhcpcdFile} >/dev/null static domain_name_servers=${IPv4dns}" | $SUDO tee -a ${dhcpcdFile} >/dev/null
} }
setStaticIPv4(){ setStaticIPv4(){