8
0

fix empty array check

This commit is contained in:
Benjamin Rechsteiner
2022-03-23 07:48:45 +01:00
parent a35e0455d1
commit f1024104a6

View File

@@ -17,6 +17,7 @@ ME=$(basename "${0}")
SYSLOG=false SYSLOG=false
RED='\033[0;31m' RED='\033[0;31m'
NC='\033[0m' NC='\033[0m'
declare -a ips
function usage { function usage {
echo "Usage: ${ME} <IP or Hostname>" >&2 echo "Usage: ${ME} <IP or Hostname>" >&2
@@ -111,16 +112,17 @@ function error {
function getIps { function getIps {
if [[ ${dest} =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; then if [[ ${dest} =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; then
ips=("${dest}") debug 'Detect an IP adress'
ips+=(${dest})
elif [[ ${dest} =~ ^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$ ]]; then elif [[ ${dest} =~ ^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$ ]]; then
debug 'Dectect a hostname'
local dns=$(dig +short "${dest}" | grep -v '\.$' | xargs) local dns=$(dig +short "${dest}" | grep -v '\.$' | xargs)
ips=($dns) ips+=($dns)
else else
error 'Destination argument is not a valide IP or Hostname' error 'Destination argument is not a valide IP or Hostname'
cleanup 1 cleanup 1
fi fi
declare -a ips if ! ((${#ips[@]})); then
if ((${#ips[@]})); then
error 'No IP could be resolved for this hostname' error 'No IP could be resolved for this hostname'
cleanup 1 cleanup 1
fi fi
@@ -137,19 +139,17 @@ function getGwIp {
function setRoute { function setRoute {
for ip in "${ips[@]}"; do for ip in "${ips[@]}"; do
ip_route="${ip}/32" ip_route="${ip}/32"
if command -v ip &> /dev/null; then
debug 'Get sudo permission to set the IP route' debug 'Get sudo permission to set the IP route'
if command -v ip &> /dev/null; then
sudo ip route add ${ip_route} dev ppp0 sudo ip route add ${ip_route} dev ppp0
debug "Set ip route ${ip_route} through SSLVPN"
elif command -v netstat &> /dev/null; then elif command -v netstat &> /dev/null; then
getGwIp getGwIp
debug 'Get sudo permission to set the IP route'
sudo route add ${ip_route} ${gwip} sudo route add ${ip_route} ${gwip}
debug "Set ip route ${ip_route} through SSLVPN"
else else
error 'Please install iproute2 or net-tools' error 'Please install iproute2 or net-tools'
cleanup 1 cleanup 1
fi fi
debug "Set ip route ${ip_route} through SSLVPN"
done done
} }