added network functions to new net.sh lib, moved set_system_proxy, begin of offline mode implementation
This commit is contained in:
@@ -63,6 +63,9 @@ read_commandline()
|
|||||||
"-D"|"--no-deps")
|
"-D"|"--no-deps")
|
||||||
export NO_DEPS=true
|
export NO_DEPS=true
|
||||||
;;
|
;;
|
||||||
|
"-o"|"--offline")
|
||||||
|
export OFFLINE=true
|
||||||
|
;;
|
||||||
"-P"|"--no-proxy")
|
"-P"|"--no-proxy")
|
||||||
export NO_PROXY=true
|
export NO_PROXY=true
|
||||||
;;
|
;;
|
||||||
|
|||||||
177
lib/net.sh
Normal file
177
lib/net.sh
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# Network functions
|
||||||
|
# This file is part of the init.sh project
|
||||||
|
# Copyright (c) 2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
# 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
|
||||||
@@ -33,6 +33,8 @@ Options :
|
|||||||
-k, --keep-going Continue l'execution en cas d'erreur.
|
-k, --keep-going Continue l'execution en cas d'erreur.
|
||||||
-r, --resume Reprend l'execution là ou elle s'est arrêté.
|
-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)
|
-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
|
-P, --no-proxy Ne pas utiliser de proxy lors de l'utilisation de
|
||||||
ce script (n'empêche pas leur configuration via modules)
|
ce script (n'empêche pas leur configuration via modules)
|
||||||
-D, --no-deps Ne pas vérifier les dépendances entre les modules.
|
-D, --no-deps Ne pas vérifier les dépendances entre les modules.
|
||||||
|
|||||||
15
lib/utils.sh
15
lib/utils.sh
@@ -8,7 +8,6 @@
|
|||||||
# https://opensource.org/licenses/BSD-3-Clause
|
# https://opensource.org/licenses/BSD-3-Clause
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# Define normalised time display, filename friendly
|
# Define normalised time display, filename friendly
|
||||||
stdtime()
|
stdtime()
|
||||||
@@ -49,15 +48,13 @@ export -f get_mod_name
|
|||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# Set system proxy vars
|
# Remove leading and trailing space of the given parameter
|
||||||
set_system_proxy()
|
trim()
|
||||||
{
|
{
|
||||||
# Declare proxy system vars if needed and if not already declared
|
local string="$@"
|
||||||
if [[ -n $PROXY_SRV && -n $PROXY_SRV_PORT && -z $NO_PROXY ]]; then
|
echo "$(sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'<<<"${string}")"
|
||||||
export http_proxy=${http_proxy:-"http://$PROXY_SRV:$PROXY_SRV_PORT/"}
|
unset string
|
||||||
export https_proxy=${https_proxy:-"http://$PROXY_SRV:$PROXY_SRV_PORT/"}
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
export -f set_system_proxy
|
export -f trim
|
||||||
|
|
||||||
# EOF
|
# EOF
|
||||||
|
|||||||
Reference in New Issue
Block a user