pause download if not connected to acertain public ip-adress

Want something added? Ask for it here.
Post Reply
F.Saucer
Newbie
Newbie
Posts: 36
Joined: July 13th, 2008, 11:37 am

pause download if not connected to acertain public ip-adress

Post by F.Saucer »

Hi,

I am using a vpn. I'm not always sure if i am connected to my vpn. Under status and interface options however i can view the public ip adress, but i'm not always at home to reset my vpn connection or able to notice the public ip adress when my (automated) downloads starts.

Is it possible to implement a solution where i can enter an public ip adress (or partial public ip adress) that is checked when downloading? (If possible combined with a time interval how often the public ip adress check should take place?) If the current public (partial) ip adress is not the same as the entered public (partial) ip adress the downloads go into pause-state and send a warning?
Sometimes my vpn drops out and comes back up after a certain time, so it would be perfect if the ip adress check would be continued in pause state, in the case my vpn connection comes back up, the download will automatically be resumed.

Thanks in advance!
User avatar
sander
Release Testers
Release Testers
Posts: 8811
Joined: January 22nd, 2008, 2:22 pm

Re: pause download if not connected to acertain public ip-ad

Post by sander »

If the VPN IP address is always the same, you could try filling out that IP address as "SABnzbd Host" under General. Warning: dangerous stuff as SABnzbd won't start if the IP address is not there.

If you are on Linux, a script of few lines can take care of what you want in a more safe way.
F.Saucer
Newbie
Newbie
Posts: 36
Joined: July 13th, 2008, 11:37 am

Re: pause download if not connected to acertain public ip-ad

Post by F.Saucer »

Sander,

VPN is not always the same, i travell a lot ;-)

I am on linux, but know very little about scripting. So some help would be much appreciated!. I assume the script must be adressed as a pre queue userscript (which leaves the problem of vpn dropping out in the midst of a download or greater already queued list of downloads) or is there another way/place to call such a script?

Grtz,
Najrad
User avatar
sander
Release Testers
Release Testers
Posts: 8811
Joined: January 22nd, 2008, 2:22 pm

Re: pause download if not connected to acertain public ip-ad

Post by sander »

Run this on your Linux:

Code: Select all

curl -s -4  http://self-test.sabnzbd.org/ 
It will show your public IPv4 address:

If you want to check if it starts with ... let's says ... "45.71.", run this:

Code: Select all

curl -s -4  http://self-test.sabnzbd.org/ | grep -e "^45.71" > /dev/null && echo "yes" || echo "no"
Fill that out with the VPN address range start. Does that work for you?

If so, we can proceed.
F.Saucer
Newbie
Newbie
Posts: 36
Joined: July 13th, 2008, 11:37 am

Re: pause download if not connected to acertain public ip-ad

Post by F.Saucer »

Sander,

This works for me, let's proceed!
User avatar
sander
Release Testers
Release Testers
Posts: 8811
Joined: January 22nd, 2008, 2:22 pm

Re: pause download if not connected to acertain public ip-ad

Post by sander »

And do you understand what that line does?

If so, proceed:
Fill out your own API-key, and make this work:

Pause SABnzbd:

Code: Select all

$ curl 'http://127.0.0.1:8080/api?mode=pause&apikey=b463b755ad289e4fd2e2e7319ab6eaca'
ok
Resume SABnzbd:

Code: Select all

$ curl 'http://127.0.0.1:8080/api?mode=resume&apikey=b463b755ad289e4fd2e2e7319ab6eaca'
ok
Does that work? Check on the commandline and in SABnzbd.
F.Saucer
Newbie
Newbie
Posts: 36
Joined: July 13th, 2008, 11:37 am

Re: pause download if not connected to acertain public ip-ad

Post by F.Saucer »

No, it says -sh: $: command not found. Without the $ a lot of garbage runs over the screen ..
BTW ... I assume you mean by "check in sabnzbd" to check if it really paused or restarted.. ???

Well, in second instance.... Without the $ upfront and filling out the port where sabnzbd is listining on in my computer (8800 instead of 8080)... it works ;-) :D
User avatar
sander
Release Testers
Release Testers
Posts: 8811
Joined: January 22nd, 2008, 2:22 pm

Re: pause download if not connected to acertain public ip-ad

Post by sander »

OK, good.

Now create a shell script "sab-check-public-ip.sh" in your home directory that combines the above, so something like:

Code: Select all

#!/bin/sh
curl -s -4  http://self-test.sabnzbd.org/ | grep -e "^45.71" > /dev/null && curl 'http://127.0.0.1:8080/api?mode=resume&apikey=b463b755ad289e4fd2e2e7319ab6eaca'|| curl 'http://127.0.0.1:8080/api?mode=pause&apikey=b463b755ad289e4fd2e2e7319ab6eaca'
Make the script executable:

Code: Select all

chmod +x sab-check-public-ip.sh
and run it. Does it work in both situations (VPN and no VPN)?

You can check your sabnzbd.log:

Code: Select all

$ cat ~/.sabnzbd/logs/sabnzbd.log | grep -i api | tail -2
2016-05-25 09:10:42,335::DEBUG::[interface:458] API-call from ::ffff:127.0.0.1 [curl/7.35.0] {'apikey': u'b463b755ad289e4fd2e2e7319ab6eaca', 'mode': u'pause'}
2016-05-25 09:11:23,157::DEBUG::[interface:458] API-call from ::ffff:127.0.0.1 [curl/7.35.0] {'apikey': u'b463b755ad289e4fd2e2e7319ab6eaca', 'mode': u'resume'}
F.Saucer
Newbie
Newbie
Posts: 36
Joined: July 13th, 2008, 11:37 am

Re: pause download if not connected to acertain public ip-ad

Post by F.Saucer »

Looks like magic to me!!! It works. And thanks for so many help!!

Somethings i don't understand however, can you explain to me?:
1. What's the function of the first pipe "|" in the syntax?
2. Why is this character "^" used in front of the ip
3. Am i correct in assuming that "> dev/null" sends the result of the grep match into the interpreter (i.e. true or false)
4. What is the function of "&&" in the syntax
5 i assume the double pipe "||" divides the commands to execute whether the grep test was positive or negative?
F.Saucer
Newbie
Newbie
Posts: 36
Joined: July 13th, 2008, 11:37 am

Re: pause download if not connected to acertain public ip-ad

Post by F.Saucer »

funny the cat command doens't work from any directory using ~. The cat command works however from the logs directory :)
User avatar
sander
Release Testers
Release Testers
Posts: 8811
Joined: January 22nd, 2008, 2:22 pm

Re: pause download if not connected to acertain public ip-ad

Post by sander »

Looks like magic to me!!! It works. And thanks for so many help!!

Good. But wait, there is more: put it in cron / crontab that it is run each 5 minutes:

Code: Select all

crontab -l
then

Code: Select all

crontab -e
and add a line like this:

Code: Select all

*/5 * * * *             /home/<yourusername>/sab-check-public-ip.sh
Somethings i don't understand however, can you explain to me?:
1. What's the function of the first pipe "|" in the syntax?
Pipe the output from the first command into the second command
2. Why is this character "^" used in front of the ip
Regexp for "at the beginning of the line"
3. Am i correct in assuming that "> dev/null" sends the result of the grep match into the interpreter (i.e. true or false)
No, it sends the text output into a null device (so: a black hole)
4. What is the function of "&&" in the syntax
Logical AND. So that command will only execute if the earlier command gave a return code other than 0. It's a trick.
5 i assume the double pipe "||" divides the commands to execute whether the grep test was positive or negative?
Logical OR. That command is executed if the return code was 0. It's a trick.

A lot of Unix/Linux concepts put into one command line ... ;)
F.Saucer
Newbie
Newbie
Posts: 36
Joined: July 13th, 2008, 11:37 am

Re: pause download if not connected to acertain public ip-ad

Post by F.Saucer »

finally beat the crontab... it is working ;-) Super ;D ;D
Is it also possible to add sending a notification? (par example through pushbullet via sabnzbd using an api call) 8)
There is an option in sabnzbd that notifies when nzb added/paused/resumed, but i only want to be informed about pause or resume...
User avatar
sander
Release Testers
Release Testers
Posts: 8811
Joined: January 22nd, 2008, 2:22 pm

Re: pause download if not connected to acertain public ip-ad

Post by sander »

Google "pushbullet linux command line" ... that gives hopeful results.

As soon as you have that working on the command line, you can add it the script. That will take same extra lines of codes, as you have to check the current status, as you only want to send a line if you change the state.
F.Saucer
Newbie
Newbie
Posts: 36
Joined: July 13th, 2008, 11:37 am

Re: pause download if not connected to acertain public ip-ad

Post by F.Saucer »

Ran into ssl certificate problems and so on... Running on a qnap nas and not familiar enough with all the packages and solutions. It seems entware-ng has a solution, but i had my share for today. Afraid to disturb the running configuration. Would be nice if the notification in sabnzbd could be splitted so that nzb added, paused and resumed could be set as individual notification settings.

Thanks for all your help!
Post Reply