fix empty array check
This commit is contained in:
@@ -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"
|
||||||
|
debug 'Get sudo permission to set the IP route'
|
||||||
if command -v ip &> /dev/null; then
|
if command -v ip &> /dev/null; then
|
||||||
debug 'Get sudo permission to set the IP route'
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user