diff --git a/lib/command_line.sh b/lib/command_line.sh index e603fb5..ca175df 100644 --- a/lib/command_line.sh +++ b/lib/command_line.sh @@ -63,6 +63,9 @@ read_commandline() "-D"|"--no-deps") export NO_DEPS=true ;; + "-o"|"--offline") + export OFFLINE=true + ;; "-P"|"--no-proxy") export NO_PROXY=true ;; diff --git a/lib/net.sh b/lib/net.sh new file mode 100644 index 0000000..d27cd26 --- /dev/null +++ b/lib/net.sh @@ -0,0 +1,177 @@ +# ------------------------------------------------------------------------------ +# Network functions +# This file is part of the init.sh project +# Copyright (c) 2022 Geoffray Levasseur +# ------------------------------------------------------------------------------ +# This file is distributed under 3-clause BSD license. +# The complete license agreement can be obtained at: +# https://opensource.org/licenses/BSD-3-Clause +# ------------------------------------------------------------------------------ + +# Network interfaces list +export IF_LIST='' + +# IPv4 vars: +export DEF_IP4='' +export DEF_CIDR4='' +export DEF_NETMASK4='' +export DEF_ROUTE4='' + +# IPv6 vars +export DEF_IP6='' +export DEF_CIDR6='' +export DEF_ROUTE6='' + +# ------------------------------------------------------------------------------ +# Set system proxy vars +set_system_proxy() +{ + # Declare proxy system vars if needed and if not already declared + if [[ -n $PROXY_SRV && -n $PROXY_SRV_PORT && -z $NO_PROXY ]]; then + export http_proxy=${http_proxy:-"http://$PROXY_SRV:$PROXY_SRV_PORT/"} + export https_proxy=${https_proxy:-"http://$PROXY_SRV:$PROXY_SRV_PORT/"} + fi +} +export -f set_system_proxy + + +# ------------------------------------------------------------------------------ +# Convert a netmask to a CIDR notation (IPv4) +mask2cidr4() +{ + # Assumes there's no "255." after a non-255 byte in the mask + local x=${1##*255.} + set -- 0^^^128^192^224^240^248^252^254^ $(( (${#1} - ${#x})*2 )) ${x%%.*} + x=${1%%$3*} + echo $(( $2 + (${#x}/4) )) +} +export -f mask2cidr4 + + +# ------------------------------------------------------------------------------ +# Convert a CIDR notation to a netmask (IPv4) +cidr2mask4() +{ + # Number of args to shift, 255..255, first non-255 byte, zeroes + set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0 + [[ $1 -gt 1 ]] && shift $1 || shift + echo ${1-0}.${2-0}.${3-0}.${4-0} +} +export -f cidr2mask4 + + +# ------------------------------------------------------------------------------ +# Determine if parameter is a valid IPv4 address +isipv4 () +{ + # Set up local variables + local ip=$1 + + # Start with a regex format test + if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then + local old_ifs=$IFS + IFS="." + ip=($ip) + IFS=$old_ifs + if [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \ + && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]; then + return 0 + fi + fi + return 1 +} +export -f isipv4 + + +# ------------------------------------------------------------------------------ +# Determine if parameter is a valid IPv4 address +isipv6 () +{ + local ip="$1" + local regex='^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$' + if [[ $ip =~ $regex ]]; then + return 0 + fi + return 1 +} +export -f isipv6 + + +# ------------------------------------------------------------------------------ +# Extract network information +get_network_info() +{ + # Get info on v4 net first + export DEF_ROUTE4=$(ip route | awk '/default/ { print $3 }') + export DEF_IF4=$(ip route | awk '/default/ { print $5 }') + export DEF_IP4=$(ip -o route get 1.1.1.1 | cut -d " " -f 7) + export DEF_CIDR4=$(ip addr show | grep -w inet | grep -v 127.0.0.1 | \ + awk '{ print $2}' | cut -d "/" -f 2) + export DEF_NETMASK4=$(cidr2mask4 $DEF_CIDR4) + export IP4_LIST=$(ip addr show | grep -w inet | grep -v 127.0.0.1 | \ + awk '{ print $2}' | cut -d "/" -f 1) + + # Now get v6 net infos + export DEF_ROUTE6=$(ip -6 route | awk '/default/ { print $3 }') + export DEF_IF6=$(ip -6 route | awk '/default/ { print $5 }') + export DEF_IP6=$(ip -6 -o route get 2001:503:ba3e::2:30 | cut -d " " -f 11) + export DEF_CIDR6=$(ip addr show | grep -w inet6 | grep -v ::1 | \ + awk 'FNR==1 { print $2}' | cut -d "/" -f 2) + export IP6_LIST=$(ip addr show | grep -w inet6 | grep -v ::1 | \ + awk '{ print $2}' | cut -d "/" -f 1) + + for iface in $(ip -o -4 addr list | awk '{print $2}' | tr '\n' ' '); do + export IF_LIST=$(trim "$IF_LIST $iface") + ipaddr=$(ip -o -4 addr list $iface | awk '{print $4}' | cut -d/ -f1) + export IP4_LIST + done + for iface in $(ip -o -6 addr list | awk '{print $2}' | tr '\n' ' '); do + if [[ ! $(echo $IF_LIST | grep $iface) ]]; then + export IF_LIST=$(trim "$IF_LIST $iface") + fi + ipaddr=$(ip -o -6 addr list $iface | awk '{print $4}' | cut -d/ -f1) + export IP4_LIST + done +} +export -f get_network_info + + +# ------------------------------------------------------------------------------ +# Check availability of different aspect of the network +check_network() +{ + case $1 in + "print") + if [[ -n $DEF_IP4 ]]; then + prnt I "Your default (routed) address is: $DEF_IP4/$DEF_CIDR4 ($DEF_NETMASK4) on $DEF_IF4" + prnt I "Your default IPv4 route is: $DEF_ROUTE4" + fi + if [[ -n $DEF_IP6 ]]; then + prnt I "Your default (routed) IPv6 address is: $DEF_IP6/$DEF_CIDR6 on $DEF_IF6" + prnt I "Your default IPv6 route is: $DEF_ROUTE6" + fi + prnt I "Here is the full list of detected v4 addresses:" + echo -e " +------------+-------------------------------------------+" + echo -e " | Interface | IP Address |" + echo -e " +------------+-------------------------------------------+" + for iface in $IF_LIST; do + ipaddr=$(ip -o -4 addr list $iface | awk '{print $4}') + for addr in $ipaddr; do + printf " |%11s | %-42s|\n" $iface $addr + done + ipaddr=$(ip -o -6 addr list $iface | awk '{print $4}') + for addr in $ipaddr; do + printf " |%11s | %-42s|\n" $iface $addr + done + done + echo " +------------+-------------------------------------------+" + ;; + "lan") + return 0 + ;; + "ternet") + return 0 + ;; + esac +} +export -f check_network diff --git a/lib/support.sh b/lib/support.sh index 7d1ed50..228b518 100644 --- a/lib/support.sh +++ b/lib/support.sh @@ -33,6 +33,8 @@ Options : -k, --keep-going Continue l'execution en cas d'erreur. -r, --resume Reprend l'execution là ou elle s'est arrêté. -R, --no-root-check Ne pas vérifier les droits root (ou UID 0) + -o, --offline Assume that all needed resource are available on a LAN + and avoid any Internet connectivity checks. -P, --no-proxy Ne pas utiliser de proxy lors de l'utilisation de ce script (n'empêche pas leur configuration via modules) -D, --no-deps Ne pas vérifier les dépendances entre les modules. diff --git a/lib/utils.sh b/lib/utils.sh index 80db1d7..498cbfc 100644 --- a/lib/utils.sh +++ b/lib/utils.sh @@ -8,7 +8,6 @@ # https://opensource.org/licenses/BSD-3-Clause # ------------------------------------------------------------------------------ - # ------------------------------------------------------------------------------ # Define normalised time display, filename friendly stdtime() @@ -49,15 +48,13 @@ export -f get_mod_name # ------------------------------------------------------------------------------ -# Set system proxy vars -set_system_proxy() +# Remove leading and trailing space of the given parameter +trim() { - # Declare proxy system vars if needed and if not already declared - if [[ -n $PROXY_SRV && -n $PROXY_SRV_PORT && -z $NO_PROXY ]]; then - export http_proxy=${http_proxy:-"http://$PROXY_SRV:$PROXY_SRV_PORT/"} - export https_proxy=${https_proxy:-"http://$PROXY_SRV:$PROXY_SRV_PORT/"} - fi + local string="$@" + echo "$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'<<<"${string}")" + unset string } -export -f set_system_proxy +export -f trim # EOF