Hey guys!
I am trying to write a bash script which changes the listen_ports depending on the result of a CURL request (the port my VPN provider uses for forwarding changes if connection is lost)
Im very new to scripting so im not sure what I am doing wrong but I suspect its because its sending the variable names to Deluge as apposed to the variable values. This is the part of the scirpt I cant get to work:
deluge-console "config --set listen_ports ($PORT,$PORT)"
I suspect it is treating the variable names as a string but I have tried all kinds of ways to comment them out so they show up as variables with no success. can anyone recomend how I could get this to work?
Thanks
Bash script to change the listen_ports
-
- Member
- Posts: 17
- Joined: Tue Jul 09, 2019 11:20 pm
Re: Bash script to change the listen_ports
I myself use:
You obviously cannot directly use that, but just for reference. Anyway, your issue is that you need to have '$PORT' defined before you can use it, just like in my example where I instead use the variable-name 'json'.
Code: Select all
json=$(curl 'xxxxxxxxxx' 2>/dev/null)
deluge-console "config -s random_port false; config -s listen_ports ($(echo ${json:26:5}), $(echo ${json:26:5}))"
You obviously cannot directly use that, but just for reference. Anyway, your issue is that you need to have '$PORT' defined before you can use it, just like in my example where I instead use the variable-name 'json'.
-
- Member
- Posts: 17
- Joined: Tue Jul 09, 2019 11:20 pm
Re: Bash script to change the listen_ports
Hi mhertz
The thing is the script already defines $PORT before that part of the code is ran please see full code below:
Im not 100% on what the error sections do but its the port_forward_assignment method which I have adapted from another script. As you can see the $PORT variable is declared before the deluge-client line. Is it the way I have used the variable? I notice you have curly brackets round yours?
Thanks for your help
The thing is the script already defines $PORT before that part of the code is ran please see full code below:
Code: Select all
error( )
{
echo "$@" 1>&2
exit 1
}
error_and_usage( )
{
echo "$@" 1>&2
usage_and_exit 1
}
usage( )
{
echo "Usage: `dirname $0`/$PROGRAM"
}
usage_and_exit( )
{
usage
exit $1
}
version( )
{
echo "$PROGRAM version $VERSION"
}
port_forward_assignment( )
{
echo 'Loading port forward assignment information...'
if [ "$(uname)" == "Linux" ]; then
client_id=`head -n 100 /dev/urandom | sha256sum | tr -d " -"`
fi
if [ "$(uname)" == "Darwin" ]; then
client_id=`head -n 100 /dev/urandom | shasum -a 256 | tr -d " -"`
fi
json=`curl "http://209.222.18.222:2000/?client_id=$client_id" 2>/dev/null`
if [ "$json" == "" ]; then
json='Port forwarding is already activated on this connection, has expired, or you are not connected to a PIA region that supports port forwarding'
fi
PORT=$(echo $json | awk 'BEGIN{r=1;FS="{|:|}"} /port/{r=0; print $3} END{exit r}')
deluge-console "config -s listen_ports ($PORT,$PORT)"
echo $PORT
echo $json
}
EXITCODE=0
PROGRAM=`basename $0`
VERSION=2.1
while test $# -gt 0
do
case $1 in
--usage | --help | -h )
usage_and_exit 0
;;
--version | -v )
version
exit 0
;;
*)
error_and_usage "Unrecognized option: $1"
;;
esac
shift
done
port_forward_assignment
exit 0
Thanks for your help
Re: Bash script to change the listen_ports
Hmm, yeah I see you have defined the port beforehand so it looks right but I cannot test as doesn't have a PIA VPN subscription anymore.
However, before I changed to another VPN provider, then I had PIA and I just found the lines I used and so know those works:
(I see now I actually don't need the 'echo' commands in there, but doesn't hurt either though, just redundant.)
The curly-braces is because of using parameter-expansion to getting solely the number extracted from the variable(and not the "Port: " part), instead of using 'cut' or 'sed' or 'awk' etc, but that's a "special" thing, and not something you normally need when using variables.
Good luck
However, before I changed to another VPN provider, then I had PIA and I just found the lines I used and so know those works:
Code: Select all
#!/bin/bash
client_id=`head -n 100 /dev/urandom | sha256sum | tr -d " -"`
json=`curl "http://209.222.18.222:2000/?client_id=$client_id" 2>/dev/null`
deluge-console "config --set listen_ports ($(echo ${json:8:5}), $(echo ${json:8:5}))"
The curly-braces is because of using parameter-expansion to getting solely the number extracted from the variable(and not the "Port: " part), instead of using 'cut' or 'sed' or 'awk' etc, but that's a "special" thing, and not something you normally need when using variables.
Good luck
-
- Member
- Posts: 17
- Joined: Tue Jul 09, 2019 11:20 pm
Re: Bash script to change the listen_ports
That is very handy to know thanks!
I think the script does actually work and its openVPN which is failing to run the script on the route-up command, not sure why though
Out of interest is the :8:5 somthing to do with selecting only the number also?
I think the script does actually work and its openVPN which is failing to run the script on the route-up command, not sure why though
Out of interest is the :8:5 somthing to do with selecting only the number also?
Re: Bash script to change the listen_ports
You're welcome, let me know if I can help you otherwise, I'm sorry I don't know why the script won't run then. Indeed, it means only output from the 8th character and then also the next 5 ones(port-number is always 5 chars here), it's quicker than using sed or cut to cut the number out and ditch the text(I took the script provided by PIA and simplified it/cut cruft out).
-
- Member
- Posts: 17
- Joined: Tue Jul 09, 2019 11:20 pm
Re: Bash script to change the listen_ports
Do you use OpenVPN to run the script or do you just run it manually?
Re: Bash script to change the listen_ports
I used to let openvpn run a script for me, but currently I have a script defined to run every time a torrent or magnet is selected in a browser, which then starts the VPN if not running already and gets port forwarded etc adds torrent, and then another script to start VPN with port and deluge afterwards to use on-demand, as I don't torrent full-time. I use a split-setup as only want the torrenting to go through VPN and everything else unprotected for better speed/performance.
Have you script-security 2 enabled in config and you need full path to up/down script even though it's in openvpn dir.
Run openvpn from terminal and check output and/or log files to see what the issue is.
Have you script-security 2 enabled in config and you need full path to up/down script even though it's in openvpn dir.
Run openvpn from terminal and check output and/or log files to see what the issue is.
-
- Member
- Posts: 17
- Joined: Tue Jul 09, 2019 11:20 pm
Re: Bash script to change the listen_ports
Thanks for the suggestions, script-secutity 2 is enabled and full path is being used.
I have run the congifg in verb 3. The script starts but hangs on the first bit where it echos 'Loading port forward assignment information...'
Ill see if I can simplify the code like yours, see if that helps.
I have run the congifg in verb 3. The script starts but hangs on the first bit where it echos 'Loading port forward assignment information...'
Ill see if I can simplify the code like yours, see if that helps.
Re: Bash script to change the listen_ports
Sorry, I still don't know the solution, but will post back if finding the answer. I tested adding my own port-forwarding lines in a script added to be run by openvpn and as you state, it dosen't work. After a lot of testing, it still doesn't work, but here's some pointers atleast.
You need full path to commands in the script. Also, I read that the script run from 'up' needs to finish before openvpn will let any packets out on the interface - that sounds strange since the directive is named 'up' but whatever. It was recommended to run another script from the original script, as it would then bypass this issue, so in the first script you e.g. add just:And then add the "real" script in 'vpn2' script instead. Still no luck for me as of yet
Good luck, and sorry I couldn't help solve this, yet atleast.
You need full path to commands in the script. Also, I read that the script run from 'up' needs to finish before openvpn will let any packets out on the interface - that sounds strange since the directive is named 'up' but whatever. It was recommended to run another script from the original script, as it would then bypass this issue, so in the first script you e.g. add just:
Code: Select all
exec /etc/openvpn/client/vpn2 &
Good luck, and sorry I couldn't help solve this, yet atleast.