scientificlinuxforum.org QR code
Scientific Linux Forum.org



  Reply to this topicStart new topicStart Poll

> How to run a script on dhcp address change
smithgcovert
 Posted: May 14 2011, 06:55 PM
Quote Post


SLF Rookie
*

Group: Members
Posts: 17
Member No.: 148
Joined: 10-May 11










So, I'm setting up my new SL 6 server, and one problem I've run into is that I can't find a new equivalent of something I used to do:

I have a custom firewall script that I used to run each time the dhcp address to the internet changed. This worked great, as I just had to drop a reference to it in dhclient-exit-hook

But the new way networks work in SL6 doesn't seem to have this same facility.

I did find (through plain old trial and error) that if I dropped the script in /etc/dhcp/dhclient.d, that it would run each time the device goes up/down.

That's getting me by right now, as my address doesn't really ever change. but it would be nice to figure out where I would put such function in the new environment.

(I did a lot of searches, but everything I found referenced Fedora 12-14 and looked really hacky)

Thanks,
Greg
PM
^
thomei
 Posted: May 14 2011, 07:28 PM
Quote Post


SLF Member
***

Group: Members
Posts: 81
Member No.: 38
Joined: 12-April 11









SL 6 uses the NetworkManager by default. May you could turn it of.

Or could write a script how monitors the address in /etc/resolv.conf . This could runs as a daemon. It could call your custom firewall script.

To start the daemon add
/etc/init.d/name (replace "name" with the name of your daemon.)
QUOTE
#!/bin/bash
### BEGIN INIT INFO                                                                                                   
# Provides: name                                                                                                       
# Default-Start: 3 5                                                                                                     
# Default-Stop: 0 1 2 6                                                                                           
# Short-Description: start the nas daemon                                                                             
# Description: steves config sharing service                                                                           
### END INIT INFO                                                                                                     
# Only for SL                                                                                                     
# chkconfig: 345 88 88                                                                                                 
                                                                                                                       
#Servicename                                                                                                           
PATH=$PATH:/bin                                                                                                       
SERVICE=ujob
PROG=/bin/name

#Source function library.
. /etc/init.d/functions

#OPTIONENPATH="/home/exchange .name"
[ -x $PROG ] || exit 0
[ -f /usr/bin/pgrep ] || exit 0
[ -f /usr/bin/pkill ] || exit 0

RETVAL=0

start() {
    #if running return 0

    if $(/usr/bin/pgrep -f $PROG >/dev/null) ; then
        echo -n $"ujob is running"
        failure
        return 0
    fi
    echo -n $"Starting name"
    $PROG
    RETVAL=$?
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$SERVICE
    [ $RETVAL -eq 0 ] && success || failure
    echo
    return $RETVAL
}

stop() {
    #if not running return 0
    if ! $(/usr/bin/pgrep -f $PROG >/dev/null) ; then
        echo -n $"name is not running"
        failure
        return 0
    fi
    echo -n $"Stoping name"
    /usr/bin/pkill -f $PROG > /dev/null
    /usr/bin/pkill -f /usr/bin/nc > /dev/null
    RETVAL=$?
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$SERVICE
    [ $RETVAL -eq 0 ] && failure || success
    echo
    return $RETVAL
}


status() {
    /usr/bin/pgrep -f $PROG > /dev/null
    RETVAL=$?
    [ $RETVAL -eq 0 ] && echo "$SERVICE is running..." || echo "$SERVICE is stopped"
    return $RETVAL
}
restart() {
    stop
    start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status
        ;;
  restart)
        stop
        start
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart}"
        exit 1
esac

exit $?

Create the daemon script itself:
/bin/name
QUOTE
UHOME=$1

(
while true
do

# add the daemon code hire

done
) </dev/null >/dev/null 2>&1 &


--------------------
thomei
think free, think Linux
PM
^
smithgcovert
 Posted: May 15 2011, 04:13 PM
Quote Post


SLF Rookie
*

Group: Members
Posts: 17
Member No.: 148
Joined: 10-May 11










Hi Thomei,

I might be able to use something like this. I can't monitor resolv.conf, because that stays constant. (I use an internal caching DNS server.)

I could monitor the ip address changes, though instead.

I also found that I can add a file named "dhclient-eth1.conf" to /etc/dhcp, and that file will be used to setup options for that dhcp client when that interface comes up.

I figured for sure that there was at least some dhcp client option that I could use to run a script on IP change, but I haven't found it yet.

Cheers,
Greg
PM
^
Jcink
 Posted: May 18 2011, 06:32 PM
Quote Post


SLF IRC Team
****

Group: Members
Posts: 253
Member No.: 15
Joined: 10-April 11









This should work for you, I just made it. It requires you have PHP installed on your SL.

Set up a cron to run this script every minute, 15 minutes, or what have you:

CODE
#!/usr/bin/php -c /etc/php-cmd.ini
<?php
$current_ip = file_get_contents("http://www.whatismyip.com/automation/n09230945.asp");

if(!$current_ip) {
die();
}

$current_ip_stored=file_get_contents("current_ip.txt");

if($current_ip != $current_ip_stored) {

system("/home/yourname/script.sh");

$f = fopen("current_ip.txt","w");
fwrite($f, trim($current_ip_stored));
fclose($f);

echo "Updated IP";
} else {
echo "No IP change found";
}
?>


Try just running it once yourself from terminal to make sure it works. It should make a file called current_ip.txt; and the next time you run it, it should print "no ip change found."

In the system(); call you can put the path to the script you want to run.
PMUsers Website
^
smithgcovert
 Posted: May 19 2011, 09:50 PM
Quote Post


SLF Rookie
*

Group: Members
Posts: 17
Member No.: 148
Joined: 10-May 11










Cool -- Thanks!
PM
^
aboron
 Posted: Oct 8 2011, 09:38 PM
Quote Post


SLF Newbie


Group: Members
Posts: 1
Member No.: 911
Joined: 8-October 11









I recently ran across the same trouble, needing to update some things whenever my home ISP shuffles me around.
And since there are a million ways to solve this, I ended up writing a shell script. I made it pretty generic so I can reuse it on different configurations.
Here's the main script I run from cron:

CODE

#!/bin/bash
NUMPARAMS=3
if [ $# -lt "$NUMPARAMS" ]
then
 echo "Usage:  checkip dev save-file change-script"
 echo "   eg:  checkip eth0 /etc/dhcp/current_ip.txt /etc/dhcp/ip_changed_script"
 echo ""
 echo "   change-script will be called with the new ip as a parameter"
 echo "   This script is best run from crontab every few minutes"
 exit 0
fi

current_ip=`/sbin/ifconfig "$1" | /bin/grep 'inet addr:[0-9]' | /usr/bin/tr -s " " | /bin/cut -d":" -f2 | /bin/cut -d" " -f1`
if [ -z "$current_ip" ]
then
 exit 0
fi

touch "$2"

last_ip=`cat "$2"`
if [ "$current_ip" != "$last_ip" ]
then
 echo "$current_ip" > "$2"
 $3 $current_ip
fi



It tracks the last ip in a file you can specify on the command line, you can also specify the interface to check and the name of the script to run when it detects a change.
In my case, I have it call this script:

CODE

#!/bin/bash
TTL=3600
SERVER=ns2.example.com
ZONE=example.com.
HOSTNAME=home.example.com.
KEYFILE=/etc/dhcp/Khome.example.com.+123+45678.key

nsupdate -v -k $KEYFILE > /dev/null << EOF
server $SERVER
zone $ZONE
update delete $HOSTNAME A
update add $HOSTNAME $TTL A $1
send
EOF

wget --delete-after --no-check-certificate "https://user:pass@ipv4.tunnelbroker.net/ipv4_end.php?tid=1000" > /dev/null 2>&1




Mod's edit: changed some of the words used of the second line.
PM
^
smithgcovert
 Posted: Dec 8 2011, 02:38 AM
Quote Post


SLF Rookie
*

Group: Members
Posts: 17
Member No.: 148
Joined: 10-May 11










For those that may come back and find this again:

I ended up finding a nice way to do this, that didn't require constantly monitoring the ip address.

It turns out you can drop a script in this directory:

/etc/dhcp/dhclient.d

Whenever the dhcp client renews the ip address, it will call all of the scripts in that directory. For me, I just dropped a script in there named "hook.sh" that resets the firewall on dhcp renew.

Cheers,
Greg
PM
^
helikaon
 Posted: Dec 9 2011, 10:15 AM
Quote Post


SLF Moderator
******

Group: Moderators
Posts: 521
Member No.: 4
Joined: 8-April 11









Nice work guys smile.gif


--------------------
PMEmail Poster
^
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:

Topic Options Reply to this topicStart new topicStart Poll