From cce15b7f0880f9713452d1c0c2eda2fa0141d172 Mon Sep 17 00:00:00 2001 From: hwdsl2 Date: Fri, 23 Sep 2016 00:39:36 -0500 Subject: [PATCH] Improve IP checking - Use a function to simplify code for IP checking - Remove new lines before matching with IP regex --- vpnsetup.sh | 25 +++++++++---------------- vpnsetup_centos.sh | 25 +++++++++---------------- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/vpnsetup.sh b/vpnsetup.sh index 36777bd..5e3b24f 100755 --- a/vpnsetup.sh +++ b/vpnsetup.sh @@ -35,6 +35,10 @@ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" exiterr() { echo "Error: ${1}" >&2; exit 1; } exiterr2() { echo "Error: 'apt-get install' failed." >&2; exit 1; } +check_ip() { + 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])$" + printf %s "${1}" | tr -d '\n' | grep -Eq "$IP_REGEX" +} os_type="$(lsb_release -si 2>/dev/null)" if [ "$os_type" != "Ubuntu" ] && [ "$os_type" != "Debian" ] && [ "$os_type" != "Raspbian" ]; then @@ -124,22 +128,11 @@ PRIVATE_IP=${VPN_PRIVATE_IP:-''} [ -z "$PRIVATE_IP" ] && PRIVATE_IP=$(ip -4 route get 1 | awk '{print $NF;exit}') # 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 +check_ip "$PUBLIC_IP" || PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://whatismyip.akamai.com) +check_ip "$PUBLIC_IP" || PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com) +check_ip "$PUBLIC_IP" || exiterr "Cannot find valid public IP. Edit the script and manually enter IPs." +check_ip "$PRIVATE_IP" || PRIVATE_IP=$(ifconfig eth0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*') +check_ip "$PRIVATE_IP" || exiterr "Cannot find valid private IP. Edit the script and manually enter IPs." # Install necessary packages apt-get -yq install libnss3-dev libnspr4-dev pkg-config libpam0g-dev \ diff --git a/vpnsetup_centos.sh b/vpnsetup_centos.sh index 4792f8d..8e97b5f 100755 --- a/vpnsetup_centos.sh +++ b/vpnsetup_centos.sh @@ -35,6 +35,10 @@ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" exiterr() { echo "Error: ${1}" >&2; exit 1; } exiterr2() { echo "Error: 'yum install' failed." >&2; exit 1; } +check_ip() { + 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])$" + printf %s "${1}" | tr -d '\n' | grep -Eq "$IP_REGEX" +} if [ ! -f /etc/redhat-release ]; then exiterr "This script only supports CentOS/RHEL." @@ -111,22 +115,11 @@ PRIVATE_IP=${VPN_PRIVATE_IP:-''} [ -z "$PRIVATE_IP" ] && PRIVATE_IP=$(ip -4 route get 1 | awk '{print $NF;exit}') # 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 +check_ip "$PUBLIC_IP" || PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://whatismyip.akamai.com) +check_ip "$PUBLIC_IP" || PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com) +check_ip "$PUBLIC_IP" || exiterr "Cannot find valid public IP. Edit the script and manually enter IPs." +check_ip "$PRIVATE_IP" || PRIVATE_IP=$(ifconfig eth0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*') +check_ip "$PRIVATE_IP" || exiterr "Cannot find valid private IP. Edit the script and manually enter IPs." # Add the EPEL repository yum -y install epel-release || exiterr2