From 4c6de2af292f012995e8fc94b0f5ef1cff7dd441 Mon Sep 17 00:00:00 2001 From: hwdsl2 Date: Mon, 10 Oct 2016 02:55:01 -0500 Subject: [PATCH] Improve network interfaces - Better handling of non-eth0 network interfaces - Now easier to use on servers with new interface names --- vpnsetup.sh | 56 ++++++++++++++++++++++++++-------------------- vpnsetup_centos.sh | 56 ++++++++++++++++++++++++++-------------------- 2 files changed, 64 insertions(+), 48 deletions(-) diff --git a/vpnsetup.sh b/vpnsetup.sh index 4ae281c..034f041 100755 --- a/vpnsetup.sh +++ b/vpnsetup.sh @@ -53,15 +53,23 @@ if [ "$(id -u)" != 0 ]; then exiterr "Script must be run as root. Try 'sudo sh $0'" fi -eth0_state=$(cat /sys/class/net/eth0/operstate 2>/dev/null) -if [ -z "$eth0_state" ] || [ "$eth0_state" = "down" ]; then +NET_IF0=${VPN_IFACE:-'eth0'} +NET_IFS=${VPN_IFACE:-'eth+'} + +if_state=$(cat "/sys/class/net/$NET_IF0/operstate" 2>/dev/null) +if [ -z "$if_state" ] || [ "$if_state" = "down" ] || [ "$NET_IF0" = "lo" ]; then + echo "Error: Network interface '$NET_IF0' is not available." >&2 cat 1>&2 <<'EOF' -Error: Network interface 'eth0' is not available. -Please DO NOT run this script on your PC or Mac! +DO NOT RUN THIS SCRIPT ON YOUR PC OR MAC! + +If running on a server, you may fix this error by first +finding the active network interface: +route | grep '^default' | grep -o '[^ ]*$' + +Then set this variable and re-run the script: +export VPN_IFACE="YOUR_INTERFACE" -Run 'cat /proc/net/dev' to find the active network interface, -then use it to replace ALL 'eth0' and 'eth+' in this script. EOF exit 1 fi @@ -137,7 +145,7 @@ PRIVATE_IP=${VPN_PRIVATE_IP:-''} 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" || PRIVATE_IP=$(ifconfig "$NET_IF0" | 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 @@ -290,7 +298,7 @@ EOF # Update sysctl settings if ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf; then /bin/cp -f /etc/sysctl.conf "/etc/sysctl.conf.old-$sys_dt" 2>/dev/null -cat >> /etc/sysctl.conf <<'EOF' +cat >> /etc/sysctl.conf </dev/null; then +elif ! iptables -t nat -C POSTROUTING -s 192.168.42.0/24 -o "$NET_IFS" -j SNAT --to-source "$PRIVATE_IP" 2>/dev/null; then ipt_flag=1 -elif ! iptables -t nat -C POSTROUTING -s 192.168.43.0/24 -o eth+ -m policy --dir out --pol none -j SNAT --to-source "$PRIVATE_IP" 2>/dev/null; then +elif ! iptables -t nat -C POSTROUTING -s 192.168.43.0/24 -o "$NET_IFS" -m policy --dir out --pol none -j SNAT --to-source "$PRIVATE_IP" 2>/dev/null; then ipt_flag=1 fi @@ -361,11 +369,11 @@ cat > /etc/iptables.rules < /etc/iptables.rules iptables-save >> /etc/iptables.rules fi diff --git a/vpnsetup_centos.sh b/vpnsetup_centos.sh index e815da0..773d3dc 100755 --- a/vpnsetup_centos.sh +++ b/vpnsetup_centos.sh @@ -56,15 +56,23 @@ if [ "$(id -u)" != 0 ]; then exiterr "Script must be run as root. Try 'sudo sh $0'" fi -eth0_state=$(cat /sys/class/net/eth0/operstate 2>/dev/null) -if [ -z "$eth0_state" ] || [ "$eth0_state" = "down" ]; then +NET_IF0=${VPN_IFACE:-'eth0'} +NET_IFS=${VPN_IFACE:-'eth+'} + +if_state=$(cat "/sys/class/net/$NET_IF0/operstate" 2>/dev/null) +if [ -z "$if_state" ] || [ "$if_state" = "down" ] || [ "$NET_IF0" = "lo" ]; then + echo "Error: Network interface '$NET_IF0' is not available." >&2 cat 1>&2 <<'EOF' -Error: Network interface 'eth0' is not available. -Please DO NOT run this script on your PC or Mac! +DO NOT RUN THIS SCRIPT ON YOUR PC OR MAC! + +If running on a server, you may fix this error by first +finding the active network interface: +route | grep '^default' | grep -o '[^ ]*$' + +Then set this variable and re-run the script: +export VPN_IFACE="YOUR_INTERFACE" -Run 'cat /proc/net/dev' to find the active network interface, -then use it to replace ALL 'eth0' and 'eth+' in this script. EOF exit 1 fi @@ -124,7 +132,7 @@ PRIVATE_IP=${VPN_PRIVATE_IP:-''} 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" || PRIVATE_IP=$(ifconfig "$NET_IF0" | 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 @@ -284,7 +292,7 @@ EOF # Update sysctl settings if ! grep -qs "hwdsl2 VPN script" /etc/sysctl.conf; then /bin/cp -f /etc/sysctl.conf "/etc/sysctl.conf.old-$sys_dt" 2>/dev/null -cat >> /etc/sysctl.conf <<'EOF' +cat >> /etc/sysctl.conf </dev/null; then +elif ! iptables -t nat -C POSTROUTING -s 192.168.42.0/24 -o "$NET_IFS" -j SNAT --to-source "$PRIVATE_IP" 2>/dev/null; then ipt_flag=1 -elif ! iptables -t nat -C POSTROUTING -s 192.168.43.0/24 -o eth+ -m policy --dir out --pol none -j SNAT --to-source "$PRIVATE_IP" 2>/dev/null; then +elif ! iptables -t nat -C POSTROUTING -s 192.168.43.0/24 -o "$NET_IFS" -m policy --dir out --pol none -j SNAT --to-source "$PRIVATE_IP" 2>/dev/null; then ipt_flag=1 fi @@ -355,19 +363,19 @@ cat > /etc/sysconfig/iptables < /etc/sysconfig/iptables iptables-save >> /etc/sysconfig/iptables fi