2020-04-21 13:00:46 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Check if a txt is a valid ip
|
|
|
|
function valid_ip()
|
|
|
|
{
|
|
|
|
local ip=$1
|
|
|
|
local stat=1
|
|
|
|
|
|
|
|
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
|
|
|
|
OIFS=$IFS
|
|
|
|
IFS='.'
|
2020-04-21 13:42:06 +02:00
|
|
|
ip=("$ip")
|
2020-04-21 13:00:46 +02:00
|
|
|
IFS=$OIFS
|
|
|
|
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
|
|
|
|
&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
|
|
|
|
stat=$?
|
|
|
|
fi
|
|
|
|
return $stat
|
|
|
|
}
|
|
|
|
|
|
|
|
# Services to get public ip
|
|
|
|
SERVICES=(
|
2020-04-21 13:37:36 +02:00
|
|
|
"curl --silent -sw :%{http_code} ipv4.icanhazip.com"
|
2020-04-21 13:00:46 +02:00
|
|
|
"curl --silent -sw :%{http_code} ifconfig.me"
|
2020-04-21 13:37:36 +02:00
|
|
|
"curl --silent -sw :%{http_code} -4 ifconfig.co"
|
2020-04-21 13:00:46 +02:00
|
|
|
"curl --silent -sw :%{http_code} ipecho.net/plain"
|
|
|
|
"curl --silent -sw :%{http_code} ipinfo.io/ip"
|
2020-04-21 13:23:01 +02:00
|
|
|
"curl --silent -sw :%{http_code} checkip.amazonaws.com"
|
2020-04-21 13:37:36 +02:00
|
|
|
"curl --silent -sw :%{http_code} v4.ident.me"
|
2020-04-21 13:00:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# Get public ip
|
|
|
|
for service in "${SERVICES[@]}"; do
|
|
|
|
RUN_COMMAND=$($service | tr -d '[:space:]')
|
2020-04-21 13:42:06 +02:00
|
|
|
IP=$(echo "$RUN_COMMAND" | cut -d':' -f1)
|
|
|
|
HTTP_CODE=$(echo "$RUN_COMMAND" | cut -d':' -f2)
|
2020-04-21 13:00:46 +02:00
|
|
|
|
|
|
|
if [ "$HTTP_CODE" == "200" ]; then
|
|
|
|
if valid_ip "$IP"; then
|
2020-04-21 13:42:06 +02:00
|
|
|
printf "%s" "$IP"
|
2020-04-21 13:00:46 +02:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
printf "error"
|
2020-04-21 13:42:06 +02:00
|
|
|
exit 0
|