You are not logged in.

#1 2010-01-29 15:02:40

manuC
Member
Registered: 2010-01-24
Posts: 10

[SOLVED] Triggering a script immediately upon IP address change (dhcp)

Is this possible?

I'm hosting my own website behind a dynamic ip address. I have a static homepage from my provider which I'm currently using just to link to it. A bash script generates the header and html to do that, and uploads it via FTP automatically. Currently it's on a cron job. The script stores my last ip and checks to see if it changed. It's not the most efficient way to go about things, though -- and even if I make it run as often as cron allows, it's still going to potentially cause downtimes of a minute.

Debian, or at least a distro I've used in the past, had a script-directory that (was supposed to) trigger when the dhcp deamon renegotiated a lease with the server, but I never got anything to run from within it as it should.

If anyone happens to know how to do this, and save me the trouble of having to look all over my /etc directory, I would definately be grateful.

Maybe I can emulate this by having the kernel spit out an ionotice at some point...

Last edited by manuC (2010-01-31 10:08:15)

Offline

#2 2010-01-30 11:23:18

fumbles
Member
Registered: 2006-12-22
Posts: 246

Re: [SOLVED] Triggering a script immediately upon IP address change (dhcp)

Take what you had in cron and create a file in /usr/lib/dhcpcd/dhcpcd-hooks and name it something like 31-ipchange

*should* work

This is assuming that you are talking about the client changing it's ip address and that you are using dhcpcd.

Last edited by fumbles (2010-01-30 11:24:46)

Offline

#3 2010-01-30 12:35:50

mcsilva
Member
From: Portugal
Registered: 2009-12-16
Posts: 95

Re: [SOLVED] Triggering a script immediately upon IP address change (dhcp)

I don't know what kind of internet connection you have. But if it is a ADSL connection, why not use a script in /etc/ppp/ip-up.local?
This way, any time your ppp connection is executed (when ipaddress changes), you can run anything you want (a firewall, a script sending your new ip... whatever).
This is the way I do it in my home system.

Offline

#4 2010-01-30 18:00:07

manuC
Member
Registered: 2010-01-24
Posts: 10

Re: [SOLVED] Triggering a script immediately upon IP address change (dhcp)

Thanks fumbles. That's what I was looking for.

mcsilva: cable modem. No PPP (un)fortunately. I'll keep it in mind, though... might come in handy in the future.

Offline

#5 2010-01-31 08:57:13

NeoXP
Member
From: MS Matrix
Registered: 2009-01-09
Posts: 206
Website

Re: [SOLVED] Triggering a script immediately upon IP address change (dhcp)

@manuC:

Care to share your script? Seems something I could use for my homeserver to.

Thx


Arch x86_64 on HP 6820s and on HP nx9420. Registered Linux User 350155, since 24-03-2004
"Everyone said that it could not be done, until someone came along who didn't know that."

Offline

#6 2010-01-31 09:21:03

manuC
Member
Registered: 2010-01-24
Posts: 10

Re: [SOLVED] Triggering a script immediately upon IP address change (dhcp)

You'll have to change the curl line to point to your FTP space, and use your name and password. I have this softlinked to from cron.hourly at the moment. I'm yet to test it with the trigger directory but you should be able to get what you need out of this and adapt it to your own situation.

#!/bin/bash
# Script that updates the meta information for the referring, public-viewable HTML page

# 1. check if IP changed since last update
# 2. If not, die
# 3. if it has, upload new HTML file.

TODATE="$(date +%Y)-$(date +%m)-$(date +%d)"
# DEBUG 
SCRIPTLOG=/home/manu/scripts/runlog
# RUN IN FOLDER WHERE PERSISTENT DATA CAN BE STORED.
cd /home/manu/scripts/persistent

if [ -f $SCRIPTLOG ] ; then
    echo "$TODATE [$(date +%T)]: Starting: updateip" >> $SCRIPTLOG
else
    echo "$TODATE [$(date +%T)]: Logfile created" >> $SCRIPTLOG
    echo "$TODATE [$(date +%T)]: Starting: updateip" >> $SCRIPTLOG
fi

echo $(ifconfig | grep inet) > newip

NEWIP=$(cut -f2 -d' ' newip)
NEWIP=${NEWIP##addr:}
OLDIP=$(cut -f2 -d' ' oldip)
OLDIP=${OLDIP##addr:}

echo "$TODATE [$(date +%T)]: * Previous IP: $OLDIP" >> $SCRIPTLOG
echo "$TODATE [$(date +%T)]: * Current  IP: $NEWIP" >> $SCRIPTLOG

if [ ! "$NEWIP" == "$OLDIP" ] ; then
    echo "<html><head><META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=http://$NEWIP:8080/mediawiki/\"><META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\" /><META HTTP-EQUIV=\"EXPIRES\" CONTENT=\"-1\" /></head><body></body></html>" >> index.html
    curl -s -T index.html -u user:password ftp://users.telenet.be
    rm index.html
    echo "$TODATE [$(date +%T)]: ==> Updated http://users.telenet.be/manucardoen/index.html" >> $SCRIPTLOG
fi
rm oldip
mv newip oldip
echo "$TODATE [$(date +%T)]: Finished: updateip" >> $SCRIPTLOG

Last edited by manuC (2010-01-31 09:24:36)

Offline

#7 2010-01-31 09:57:32

mcsilva
Member
From: Portugal
Registered: 2009-12-16
Posts: 95

Re: [SOLVED] Triggering a script immediately upon IP address change (dhcp)

If you have cable instead of ADSL, you have another option instead of cron job, and similar to what I said about ADSL.
I also had cable before ADSL, and I used "dhclient" to run my firewall, in this way:

Dhclient runs a script called "/sbin/dhclient-script" that verifies the existence of /etc/dhclient-enter-hooks" and "/etc/dhclient-exit-hooks".
You have to create them, because they don't exist by default.
$touch /etc/dhclient-enter-hooks
$chmod +x /etc/dhclient-enter-hooks
$touch /etc/dhclient-exit-hooks
$chmod +x /etc/dhclient-exit-hooks

"dhclient-enter-hooks" runs in the beginnig of the dhclient-script, and "dhclient-exit-hooks" runs in the end. So, you may chose when you want to run your script.
If you create dhclient-exit-hooks with a script like that:
if ([ $reason = "RENEW" ] && [ $old_ip_address != $new_ip_address ]) ; then
    /usr/sbin/firewall
elif ([ $reason = "REBOOT" ] || [ $reason = "BOUND" ]) ; then
    /usr/sbin/firewall
fi

So, everytime there was a "renew" in dhcp, and ip address was changed, my firewall script was run automatically.
You can read "man dhclient-script" to understand it better, and run whichever script you want when your ip address changes.

Last edited by mcsilva (2010-01-31 09:58:50)

Offline

#8 2010-01-31 10:09:23

manuC
Member
Registered: 2010-01-24
Posts: 10

Re: [SOLVED] Triggering a script immediately upon IP address change (dhcp)

Good stuff. That's yet another viable option.
I forgot to append [SOLVED]

Offline

Board footer

Powered by FluxBB