How do I execute a script after DHCP assigns an IP address on start up?

9,050

Hooks


There is an array of possibilities how to do this.

If you want to execute your script " after DHCP runs " then hooks are the right thing for you !

dhclient will execute everything in the directory /etc/dhcp/dhclient-exit-hooks.d/ after it exits.*

Its good practice to link your script to the directory, rather than place it there - but both works.

ln -s /path/to/your/script  /etc/dhcp/dhclient-exit-hooks.d/name_of_your_script

(*) However, dhclient doesn't "exit" per se, but rather continues to run and executes ("sources") this directory every-time it does something for a reason.

So, to prohibit your script from being executed numerous times, I suggest you wrap it with an "if statement" to execute it only if the IP address is bound or renewed like this:

if ([ $reason = "BOUND" ] || [ $reason = "RENEW" ])
then

# your script commands here

fi
Share:
9,050

Related videos on Youtube

dpoiesz
Author by

dpoiesz

Updated on September 18, 2022

Comments

  • dpoiesz
    dpoiesz over 1 year

    I have written a shell script that sends my current IP to a google drive folder to aid in automating logging into a raspberry PI. Another script on my laptop retrieves the IP and logs in. The script works fine when I run it from the command line but I am having trouble getting it to run on start up after the IP is assigned.

    I have tried crontab but the script runs before the IP is assigned. I do not wish to set a timer or uses a while loop to wait for an IP, that doesn't sound very efficient. I am trying to execute my script during the start up process but after DHCP runs. I tried adding the path to upstart in the allinterfaceup() function but that did not work either. Any help would be appreciated.

    Raspberry PI 3, Ubuntu Mate 16.04 LTS

    script:

    #!/bin/sh
    
    #if ([ $reason = "BOUND" ] || [ $reason = "RENEW" ] )
    #then
    
            echo `hostname -I | cut -d' ' -f1 ` > ~/ipAddr/ip.txt
    
            rclone copy ~/ipAddr/ip.txt remote:pi 
    #fi
    
    • chili555
      chili555 about 6 years
      Wouldn't it be much easier to assign a static, never changing IP address?
    • nobody
      nobody about 6 years
      You can add a loop at the beginning of your script that checks if IP is already assigned. After IP is assigned you exit the loop and proceed. I would check output of /sbin/ifconfig for instance. Do you have to use your public IP? In that case I would recommend dynamic DNS like noip.com. You can download scripts there to update DNS entries and you don't care about IPs anymore, just use names.
    • dpoiesz
      dpoiesz about 6 years
      I can not assign a static IP address and still connect to my schools or friends networks.
  • dpoiesz
    dpoiesz about 6 years
    Sounds good, I'll give it a shot and let you know how it goes. Thank you.
  • dpoiesz
    dpoiesz about 6 years
    That did not work for me. I added the scripts code to my question above. The script executes properly when I run it from the command line but had to remove your suggested if statement for it to work.
  • Robert Riedl
    Robert Riedl about 6 years
    if you add the if-statement it won't run from the commandline anymore. the $reason is only provided by dhclient in the exit-hooks-context ! If you still want to be able to execute your script from the commandline, then I suggest you make two scripts: one in the hook directory that has the if-stament and executes your original script ( like bash /path/to/your/script) and your original script (that is no longer linked into the hooks-directory).
  • dpoiesz
    dpoiesz about 6 years
    I understand that, sorry for the confusing comment. The script did not execute properly when I linked it to /etc/dhcp/dhclient-exit-hooks.d/. It does execute properly when I run it from the command line(I comment out the if statement).
  • Robert Riedl
    Robert Riedl about 6 years
    use an absolute path for ~/ipAddr/ip.txt - the script is not executed as your user and is not aware of your home dir.
  • Robert Riedl
    Robert Riedl about 6 years
    did that work for you ?
  • dpoiesz
    dpoiesz about 6 years
    Sorry, I missed the notification for your last comment. That did not work. The script doesn't have permission to write outside my user directory. I'll have to research that when I have more time. Unfortunately, the time it is taking to automate this process is reducing its utility. I appreciate you taking the time to help, thank you.
  • Robert Riedl
    Robert Riedl about 6 years
    Write to /tmp/ ?
  • dpoiesz
    dpoiesz about 6 years
    I did try /tmp/ with and without a subdirectory and recieved the following: /bin/sendIP: 6: /bin/sendIP: cannot create /tmp/ip.txt: Permission denied
  • Aloha
    Aloha about 6 years
    PSA: I found that the scripts don't execute when they have an extension. For me, on dhclient-exit-hooks.d, load_balance.sh doesn't work but load_balance works.