Reputation: 1214
I have a script that runs every hour to facilitate port forwarding with openvpn. It all works well when run from CLI, but fails when run through the same users cron. The part that fails is the end where it uses the value $PORT.
You can see that the values PORT and VPN_IP are not returning a value and the deluge command is failing.
Here is the result run directly:
Your VPN ipaddress is 10.107.1.6
Contacting PIA for port forwarding .......
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 106 100 14 100 92 4 30 0:00:03 0:00:03 --:--:-- 30
Port forwarding is currently using port 37186
Changing port settings on deluge....
Setting random_port to False..
Configuration value successfully updated.
Setting listen_ports to (37186, 37186)..
and here is the same script run through cron crontab: 34 * * * * bash /home/alleyoopster/scripts/pia_portforward.sh > /home/alleyoopster/pia_port.log 2>&1
Result with no VPN address or Port address returned and errors:
Your VPN ipaddress is
Contacting PIA for port forwarding .......
Port forwarding is currently using port
Changing port settings on deluge....
Setting random_port to False..
Configuration value successfully updated.
malformed expression (,)
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/deluge/ui/console/main.py", line 344, in do_command
ret = self._commands[cmd].handle(*args, **options.__dict__)
File "/usr/lib/python2.7/dist-packages/deluge/ui/console/commands/config.py", line 104, in handle
return self._set_config(*args, **options)
File "/usr/lib/python2.7/dist-packages/deluge/ui/console/commands/config.py", line 140, in _set_config
val = simple_eval(options["set"][1] + " " .join(args))
File "/usr/lib/python2.7/dist-packages/deluge/ui/console/commands/config.py", line 87, in simple_eval
res = atom(src.next, src.next())
File "/usr/lib/python2.7/dist-packages/deluge/ui/console/commands/config.py", line 56, in atom
out.append(atom(next, token))
File "/usr/lib/python2.7/dist-packages/deluge/ui/console/commands/config.py", line 79, in atom
raise SyntaxError("malformed expression (%s)" % token[1])
SyntaxError: malformed expression (,)
#! /bin/sh
#Simple bash script to facilitate Port Forwarding use with openvpn and PIA
#Use as a cron job to run every hour
#This script will also change the port in deluge. It needs deluge-console installed
#Transmission should also work with the correct commands
#YOUR SETTINGS
#Private Internet Access Username and Password here
USERNAME="username"
PASSWORD="password"
#Enter the correct tun here. Normally tun0. The command ifconfig will list your network config
TUN="tun0"
#Get the local ip address
VPN_IP=$(ifconfig $TUN|grep -oE "inet addr: *10\.[0-9]+\.[0-9]+\.[0-9]+"|tr -d "a-z :")
echo "Your VPN ipaddress is " $VPN_IP
echo Contacting PIA for port forwarding .......
TMP_PORT=$(curl -d "user=$USERNAME&pass=$PASSWORD&client_id=$(cat ~/.pia_client_id)&local_ip=$VPN_IP" https://www.privateinternetaccess.com/vpninfo/port_forward_assignment)
PORT=$(echo $TMP_PORT | sed "s/[^0-9]*//g")
echo "Port forwarding is currently using port "$PORT
echo "Changing port settings on deluge...."
deluge-console "config --set random_port False"
deluge-console "config --set listen_ports ($PORT,$PORT)"
Upvotes: 1
Views: 275
Reputation: 4349
It sounds like the PATH
setting in your cron job doesn't match your user's PATH
, and cron may not be finding the ifconfig
command so that it can obtain the VPN IP address.
Either specify the full path to /sbin/ifconfig
to get the local IP address, or add the following line at the top of your crontab (I'm just listing standard paths - adjust as necessary to suit your setup):
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
Upvotes: 2