Start script on change of WAN-IP possible?


Recommended Posts

Is there a way to recognize a change of the WAN IP? I would like to call a script whenever this IP changes (to re-start nzbget for example):

 

nzbget -q
nzbget -s &

 

Many thanks in advance.

Harald

 

 

For example (copy/paste the following into root user terminal session):

 

SCRIPT=/boot/custom/bin/restart_on_ip_change.sh

SCRIPT_DIR=`dirname $SCRIPT`

[ -d "$SCRIPT_DIR" ] || mkdir -p $SCRIPT_DIR

 

cp -p /boot/config/go /var/tmp

cat >>/boot/config/go <<EOF

#

# Add nzbget restart to crontab

#

crontab -l >/tmp/crontab

cp /tmp/crontab /var/spool/cron/crontabs/root-

(echo -n '0 * * * * '; echo $SCRIPT) >>/tmp/crontab

crontab /tmp/crontab

EOF

 

cat >$SCRIPT <<EOF

#!/bin/bash

IP_FILE="/var/tmp/current_ip"

NEW_ADDR=\$(wget -O - http://ipdetect.dnspark.com/ 2>/dev/null | grep Address | tr -cd '0-9.')

OLD_ADDR=\$(cat $IP_FILE)

if [ "\$NEW_ADDR" != "\$OLD_ADDR" ]; then

nzbget -q

nzbget -s &

fi

echo \$NEW_ADDR >\$IP_FILE

EOF

 

 

Link to comment

Sorry, there was an error. Here is the correct code:

 

SCRIPT=/boot/custom/bin/restart_on_ip_change.sh

SCRIPT_DIR=`dirname $SCRIPT`

[ -d "$SCRIPT_DIR" ] || mkdir -p $SCRIPT_DIR

 

cp -p /boot/config/go /var/tmp

cat >>/boot/config/go <<EOF

#

# Add nzbget restart to crontab

#

crontab -l >/tmp/crontab

cp /tmp/crontab /var/spool/cron/crontabs/root-

(echo -n '0 * * * * '; echo $SCRIPT) >>/tmp/crontab

crontab /tmp/crontab

EOF

 

cat >$SCRIPT <<EOF

#!/bin/bash

IP_FILE="/var/tmp/current_ip"

NEW_ADDR=\$(wget -O - http://ipdetect.dnspark.com/ 2>/dev/null | grep Address | tr -cd '0-9.')

OLD_ADDR=\$(cat \$IP_FILE 2>/dev/null)

if [ "\$NEW_ADDR" != "\$OLD_ADDR" ]; then

nzbget -q

nzbget -s &

fi

echo \$NEW_ADDR >\$IP_FILE

EOF

 

chmod +x $SCRIPT

 

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.