From 7937a74469c8b55a70855db6ee856ffed1205e31 Mon Sep 17 00:00:00 2001 From: hwdsl2 Date: Fri, 9 Sep 2016 15:33:12 -0500 Subject: [PATCH] Improve IP detection - Remove unneeded code for Amazon EC2 - Check IPs for correct format after each try --- vpnsetup.sh | 18 ++++++++++-------- vpnsetup_centos.sh | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/vpnsetup.sh b/vpnsetup.sh index f962f47..4599c81 100755 --- a/vpnsetup.sh +++ b/vpnsetup.sh @@ -119,22 +119,24 @@ EOF PUBLIC_IP=${VPN_PUBLIC_IP:-''} PRIVATE_IP=${VPN_PRIVATE_IP:-''} -# In Amazon EC2, these two variables will be retrieved from metadata -[ -z "$PUBLIC_IP" ] && PUBLIC_IP=$(wget -t 3 -T 5 -qO- 'http://169.254.169.254/latest/meta-data/public-ipv4') -[ -z "$PRIVATE_IP" ] && PRIVATE_IP=$(wget -t 3 -T 5 -qO- 'http://169.254.169.254/latest/meta-data/local-ipv4') - -# Try to find IPs for non-EC2 servers +# Try to auto discover IPs of this server [ -z "$PUBLIC_IP" ] && PUBLIC_IP=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short) -[ -z "$PUBLIC_IP" ] && PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://whatismyip.akamai.com) -[ -z "$PUBLIC_IP" ] && PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com) [ -z "$PRIVATE_IP" ] && PRIVATE_IP=$(ip -4 route get 1 | awk '{print $NF;exit}') -[ -z "$PRIVATE_IP" ] && PRIVATE_IP=$(ifconfig eth0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*') # Check IPs for correct format IP_REGEX="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" +if ! printf %s "$PUBLIC_IP" | grep -Eq "$IP_REGEX"; then + PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://whatismyip.akamai.com) +fi +if ! printf %s "$PUBLIC_IP" | grep -Eq "$IP_REGEX"; then + PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com) +fi if ! printf %s "$PUBLIC_IP" | grep -Eq "$IP_REGEX"; then exiterr "Cannot find valid public IP. Edit the script and manually enter IPs." fi +if ! printf %s "$PRIVATE_IP" | grep -Eq "$IP_REGEX"; then + PRIVATE_IP=$(ifconfig eth0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*') +fi if ! printf %s "$PRIVATE_IP" | grep -Eq "$IP_REGEX"; then exiterr "Cannot find valid private IP. Edit the script and manually enter IPs." fi diff --git a/vpnsetup_centos.sh b/vpnsetup_centos.sh index ddea15a..0f8afaa 100755 --- a/vpnsetup_centos.sh +++ b/vpnsetup_centos.sh @@ -106,22 +106,24 @@ EOF PUBLIC_IP=${VPN_PUBLIC_IP:-''} PRIVATE_IP=${VPN_PRIVATE_IP:-''} -# In Amazon EC2, these two variables will be retrieved from metadata -[ -z "$PUBLIC_IP" ] && PUBLIC_IP=$(wget -t 3 -T 5 -qO- 'http://169.254.169.254/latest/meta-data/public-ipv4') -[ -z "$PRIVATE_IP" ] && PRIVATE_IP=$(wget -t 3 -T 5 -qO- 'http://169.254.169.254/latest/meta-data/local-ipv4') - -# Try to find IPs for non-EC2 servers +# Try to auto discover IPs of this server [ -z "$PUBLIC_IP" ] && PUBLIC_IP=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short) -[ -z "$PUBLIC_IP" ] && PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://whatismyip.akamai.com) -[ -z "$PUBLIC_IP" ] && PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com) [ -z "$PRIVATE_IP" ] && PRIVATE_IP=$(ip -4 route get 1 | awk '{print $NF;exit}') -[ -z "$PRIVATE_IP" ] && PRIVATE_IP=$(ifconfig eth0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*') # Check IPs for correct format IP_REGEX="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" +if ! printf %s "$PUBLIC_IP" | grep -Eq "$IP_REGEX"; then + PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://whatismyip.akamai.com) +fi +if ! printf %s "$PUBLIC_IP" | grep -Eq "$IP_REGEX"; then + PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com) +fi if ! printf %s "$PUBLIC_IP" | grep -Eq "$IP_REGEX"; then exiterr "Cannot find valid public IP. Edit the script and manually enter IPs." fi +if ! printf %s "$PRIVATE_IP" | grep -Eq "$IP_REGEX"; then + PRIVATE_IP=$(ifconfig eth0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*') +fi if ! printf %s "$PRIVATE_IP" | grep -Eq "$IP_REGEX"; then exiterr "Cannot find valid private IP. Edit the script and manually enter IPs." fi