You are not logged in.

#1 2020-01-04 14:29:16

frunkaf
Member
Registered: 2020-01-04
Posts: 3

OpenVPN Client Route Script

Hello,

I'm following this tutorial to limit my VPN client connection to a single interface so I can use it with certain programs and have all other services work through my regular internet connection...

https://www.pantz.org/software/openvpn/ … rding.html

I can't run the script file provided in this tutorial because of a syntax error and missing commands.

Here is the script I'm trying to run...

#!/bin/sh
# script used by OpenVPN to setup a route on Linux.
# used in conjunction with OpenVPN config file options
# script-security 2, route-noexec, route-up 
# script also requires route table rt2
# sudo bash -c 'echo "1 rt2" >> /etc/iproute2/rt_tables'

# openvpn variables passed in via env vars
rtname="rt2"
int=$dev
iplocal=$ifconfig_local
ipremote=$ifconfig_remote
gw=$route_vpn_gateway

if [ -z $int ] || [ -z $iplocal ] || [ -z $ipremote ] || [ -z $gw ]; then
  echo "No env vars found. Use this script with an OpenVPN config file "
  exit 1
fi

help() {
  echo "For setting OpenVPN routes on Linux."
  echo "Usage: $0 up or down"
}

down() {
  # delete vpn route if found
  ip route flush table $rtname
  if [ $? -eq 0 ]; then
    echo "Successfully flushed route table $rtname"
  else
    echo "Failed to flush route table $rtname"
  fi    
}

up() {
  # using OpenVPN env vars that get set when it starts, see man page
  echo "Tunnel on interface $int. File /tmp/vpnint"
  echo $int > /tmp/vpnint
  echo "Local IP is         $iplocal. File /tmp/vpnip"
  echo $iplocal > /tmp/vpnip
  echo "Remote IP is        $ipremote"
  echo "Gateway is          $gw"

  down # remove any old routes

  ip route add default via $gw dev $int table $rtname
  if [ $? -eq 0 ]; then
    echo "Successfully added default route $gw"
  else
    echo "Failed to add default route for gateway $gw"
  fi
  ip rule add from $iplocal/32 table $rtname
  if [ $? -eq 0 ]; then
    echo "Successfully added local interface 'from' rule for $iplocal"
  else
    echo "Failed to add local interface 'from' rule for $iplocal"
  fi
  ip rule add to $gw/32 table $rtname
  if [ $? -eq 0 ]; then
    echo "Successfully added local interface 'to' rule for $gw"
  else
    echo "Failed to add local interface 'to' rule for $gw"
  fi

}

case $1 in
  "up") up;;
  "down") down;;
  *) help;;
esac

# always flush route cache 
ip route flush cache

When I do a

sudo bash -x openvpn-route.sh

I get the following output...

+ $'\r'
/etc/openvpn/surfshark/openvpn-route.sh: line 7: $'\r': command not found
+ rtname=$'rt2\r'
+ int=$'\r'
+ iplocal=$'\r'
+ ipremote=$'\r'
+ gw=$'\r'
+ $'\r'
/etc/openvpn/surfshark/openvpn-route.sh: line 14: $'\r': command not found
/etc/openvpn/surfshark/openvpn-route.sh: line 20: syntax error near unexpected token `$'{\r''
'etc/openvpn/surfshark/openvpn-route.sh: line 20: `help() {

If I'm not mistaken, Lines 7 and 14 are blank.

Line 20 is

help() {

.

This same script was executing before on debian, ubuntu, and centos. Am I missing packages? Does this need to be modified for Arch in some way?


I'd really appreciate any assistance at all. Thank you so much!

Offline

#2 2020-01-04 16:26:22

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: OpenVPN Client Route Script

Where are you setting the required environment variables? What's the output of...

env

No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2020-01-04 16:33:27

frunkaf
Member
Registered: 2020-01-04
Posts: 3

Re: OpenVPN Client Route Script

That output is...

SHELL=/bin/bash
PWD=/home/frunk
LOGNAME=frunk
XDG_SESSION_TYPE=tty
HOME=/home/frunk
LANG=C
SSH_CONNECTION=10.255.255.1 60126 10.255.255.253 31337
XDG_SESSION_CLASS=user
TERM=xterm
USER=frunk
SHLVL=1
XDG_SESSION_ID=3
XDG_RUNTIME_DIR=/run/user/1000
PS1=[\[\]\[\033[38;5;13m\]\u\[\]\[\033[38;5;14m\]@\h\[\]\[\033[38;5;15m\] \[\]\[\033[38;5;10m\]\w\[\]\[\033[38;5;15m\]]\$ \[\]
SSH_CLIENT=10.255.255.1 60126 31337
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
MAIL=/var/spool/mail/frunk
SSH_TTY=/dev/pts/0
_=/usr/bin/env

Offline

#4 2020-01-04 22:28:22

frunkaf
Member
Registered: 2020-01-04
Posts: 3

Re: OpenVPN Client Route Script

Found the solution!

https://stackoverflow.com/questions/116 … sh-profile

running dos2unix on the script made it work again.

I have yet to understand why but it works now.

Offline

Board footer

Powered by FluxBB