LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2008, 05:13 PM   #1
dlublink
Member
 
Registered: Oct 2004
Location: Canada
Distribution: Ubuntu
Posts: 330

Rep: Reputation: 30
DHCP execute script on lease


Hello,

When dhcpd ( running on Debian Etch 4 ) leases an IP address I want my script to be notified. The script will likely be a bash script. I should receive the MAC address, IP address and the lease time. I should also receive a notification if the lease is released by the customer.

I'll need root privileges for the commands executed. ( Or I could program sudo to allow me to execute the commands needed ).

How can I do this?

Thanks,

David
 
Old 11-30-2008, 11:32 AM   #2
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
Scripting

You could set a tail on the dhcpd log and pipe it into your processing.
I am running F9 so your setup is quite probably somewhat different. But the following may be helpful.

I have set my dhcpd to log into its own file but that is not really necessary as by default it will log into /var/log/messages and can be sorted out like this:
sudo tail --follow=name /var/log/messages | grep dhcpd | your-processing-script

My set up put it in its own file, /var/log/dhcpdlog by including the following:
in my /etc/dhcpd.conf
log-facility local1; # per /etc/rsyslog.conf setting

in my /etc/rsyslog.conf
# log dhcpd per /etc/dhcpd.conf
local1.* /var/log/dhcpdlog

I do have the logs rotated by /usr/sbin/logrotate (see man logrotate) by adding the following to /etc/logrotate.d/syslog:

/var/log/dhcpdlog{
sharedscripts
copytruncate
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}

Actually I added it to the existing syslog entry that included other logs that are rotated and mine now looks like this:

/var/log/dhcpdlog /var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron /var/log/update-debug.log /var/log/named-auth.info{
sharedscripts
copytruncate
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}

Then the tail command looks like this:

tail --follow=name /var/log/dhcpdlog | local-processing-script

Your script will have to look for the offer and the ack packets in this log. Here is a request, acknowledge, and inform secguence from the log:

Nov 27 07:11:51 maplepark dhcpd: DHCPREQUEST for 192.168.102.95 (192.168.102.9) from 00:c0:a8:da:94:35 (drf-PC) via eth0
Nov 27 07:11:51 maplepark dhcpd: DHCPACK on 192.168.102.95 to 00:c0:a8:da:94:35 (drf-PC) via eth0
Nov 27 07:16:22 maplepark dhcpd: DHCPINFORM from 192.168.102.114 via eth0
Nov 27 07:16:22 maplepark dhcpd: DHCPACK to 192.168.102.114 (00:00:39:1b:43:0e) via eth0

I do run dynamic updates to my bind and they show up as:
when adding:

Nov 27 09:47:30 maplepark dhcpd: DHCPDISCOVER from 00:c0:a8:da:94:35 via eth0
Nov 27 09:47:31 maplepark dhcpd: DHCPOFFER on 192.168.102.95 to 00:c0:a8:da:94:35 (drf-PC) via eth0
Nov 27 09:47:31 maplepark dhcpd: Added new forward map from drf-PC.maplepark.com to 192.168.102.95
Nov 27 09:47:31 maplepark dhcpd: added reverse map from 95.102.168.192.in-addr.arpa. to drf-PC.maplepark.com
Nov 27 09:47:31 maplepark dhcpd: Wrote 0 deleted host decls to leases file.
Nov 27 09:47:31 maplepark dhcpd: Wrote 0 new dynamic host decls to leases file.

when deleting:

Nov 27 08:11:51 maplepark dhcpd: if drf-PC.maplepark.com IN TXT "31467d5c297125ea22734a4e36c193b583" rrset exists and drf-PC.maplepark.com IN A 192.168.102.95 rrset exists delete drf-PC.maplepark.com IN A 192.168.102.95: success.
Nov 27 08:11:51 maplepark dhcpd: if drf-PC.maplepark.com IN A rrset doesn't exist delete drf-PC.maplepark.com IN TXT "31467d5c297125ea22734a4e36c193b583": success.
Nov 27 08:11:51 maplepark dhcpd: removed reverse map on 95.102.168.192.in-addr.arpa.
Nov 27 08:45:40 maplepark dhcpd: Wrote 0 deleted host decls to leases file.
Nov 27 08:45:40 maplepark dhcpd: Wrote 0 new dynamic host decls to leases file.


The interesting thing here is that, although the lease was deleted in bind at 8:11:51, the lease file was not written to until 8:45:40. I suspect that using the lease file as gospel truths may be an error as it is primarily to store information for dhcpd's use between requests and shutdowns, not as an up-to-date snapshot available for other processes to use. But your needs will dictate if you can use it. Other dhcpd daemons, fallover particularily, could also change the actual picture from what you expect.

Dave
 
Old 12-02-2008, 10:14 AM   #3
dlublink
Member
 
Registered: Oct 2004
Location: Canada
Distribution: Ubuntu
Posts: 330

Original Poster
Rep: Reputation: 30
Hello,

I did not even think of running a tail on the syslog!!

Great idea!

David
 
Old 12-16-2008, 05:21 AM   #4
Skiltron
LQ Newbie
 
Registered: Dec 2008
Location: Amsterdam, Holland
Distribution: Ubuntu and TinyMe
Posts: 4

Rep: Reputation: 0
Hey have you made the script?

I'm looking for exactly the same thing, I got the tail /var/log/syslog | grep "dhcpd: DHCPACK"
But how do i get just the ip address from the whole line:
Dec 14 12:23:09 ubuntu dhcpd: DHCPACK on 192.168.0.30 to bla bla

Thanks in advance
 
Old 12-16-2008, 01:17 PM   #5
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
Skiltron, just pipe the output of your tail /var/log/syslog | grep "dhcpd: DHCPACK" into cut
cut picks out things by their field position and is described in man cut

For your request to see the IP, it could simply look like this:
tail /var/log/syslog | grep "dhcpd: DHCPACK" | cut -d " " -f 8 | your-processing-script

You could use awk which is more powerful but requires some educational effort. Many books are available.
A simple awk:
/usr/bin/tail --follow=name /var/log/syslog | awk -f some_plugin |bash where your plug in can be quite versitle
an example might be like the routine I use to catch rogue ssh logins on my machine as detailed at:
http://maplepark.com/~drf/consults/Killit_Plan.html

But the quickest might be the "cut" example as above. You might want to add the --follow=name option to the tail for a continuous daemon.

Dave
 
Old 12-17-2008, 03:13 AM   #6
Skiltron
LQ Newbie
 
Registered: Dec 2008
Location: Amsterdam, Holland
Distribution: Ubuntu and TinyMe
Posts: 4

Rep: Reputation: 0
Thanks It works!

I got the tail, grep and cut statements and put it in a variable..
Echo the variable and there it is!
If I add the --follow that doesn't work anymore
I'd like it to run an application and give the app the ip-address

As you might have noticed I'm new to shell scripts :P
 
Old 01-04-2010, 10:55 AM   #7
dlublink
Member
 
Registered: Oct 2004
Location: Canada
Distribution: Ubuntu
Posts: 330

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by Skiltron View Post
Thanks It works!

I got the tail, grep and cut statements and put it in a variable..
Echo the variable and there it is!
If I add the --follow that doesn't work anymore
I'd like it to run an application and give the app the ip-address

As you might have noticed I'm new to shell scripts :P
No, I never made the script. I had other things come up. I'll come back to this eventually.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
DHCP lease same IP prashanlk Linux - Networking 2 01-31-2008 03:22 AM
Force DHCP To Refuse Clients A DHCP Lease Depending On Client MAC Address blackfish Linux - Server 3 08-17-2007 12:54 AM
Requesting a new DHCP lease raypen Linux - Networking 2 06-07-2006 09:19 PM
Cannot obtain DHCP lease TroelsSmit Linux - Newbie 24 06-18-2005 03:32 AM
DHCP Lease Delete! kofi Linux - Networking 2 07-29-2004 07:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 09:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration