Improve IP detection
- Remove unneeded code for Amazon EC2 - Check IPs for correct format after each try
This commit is contained in:
parent
6d4bad1fd2
commit
7937a74469
18
vpnsetup.sh
18
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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user