Quantcast
Viewing all articles
Browse latest Browse all 60861

Re: DHCP on lease script

Old thread, I know. But I came across this and it helped me figure out (thanks!) how to get notified of any time a new device (MAC) gets added to my network. I didn't really see any other comprehensive guides on how to do this, so I thought I would contribute what I have. This can send email and/or Prowl (iOS) Push notifications. This works on my ERL with v1.9.1:

root@erl:/config/scripts/dhcp# cat /config/scripts/dhcp/dhcp.sh
#!/bin/bash
#Written by Steve Rosenstein 2017
#The following lines are neccessary to be in: /opt/vyatta/sbin/on-dhcp-event.sh (at the end, before the exit 0)
#############
#DHCPfile="/config/scripts/dhcp/dhcp.sh"
#if [ -f "$DHCPfile" ];
#then
#        sh "$DHCPfile" "$action" "$client_name" "$client_ip" "$client_mac" "$domain" &
#fi
#
#############
# IMPORTANT: make sure that update-hostfile is enabled within the dhcp options
if [ $# -le "1" ]
then
	#print usage if 0 arguments given
	echo "$0 action client_name client_ip client_mac domain"
exit
fi

#Set sendemail=1 if you want to sent email, you will need to setup your ssmtp settings and make sure they work
#	See /etc/ssmtp/ssmtp.conf and /etc/ssmtp/revaliases
#Set sendprowl=1 if you want to send a prowl push notification
#################
#File where we will store all of the hostnames and MACs that have already been added
MAC_FILE="/config/scripts/dhcp/mac.file"
################
#Email settings
sendemail=0;
email_to="PUT YOUR EMAIL TO ADDRESS HERE"
email_from="PUT YOUR FROM EMAIL ADDRESS HERE"
#################
#Prowl Settings
sendprowl=1;
apikey=PUT YOUR SUPER SECRET PROWL API KEY HERE
#################
#Name our variables
action=$1;
client_name=$2;
client_ip=$3;
client_mac=$4
domain=$5;
###################
##################
do_send_email()
{
email_to=$4
email_from=$5
#write out a temp file with the info to be directed into ssmtp later
cat > /config/scripts/dhcp/message.txt << EOF
To: $email_to
From: $email_from
Subject: NEW DEVICE ADDED TO NETWORK Hostname: $1 IP: $2 MAC: $3

$(date)
Hostname:$1
IP:$2
MAC:$3

EOF
/usr/sbin/ssmtp $email_to < /config/scripts/dhcp/message.txt
}

#Make sure $MAC_FILE exists or touch it
if [ ! -f $MAC_FILE ]
then
	touch $MAC_FILE || exit 1;
fi

#See if the $client_mac passed to the script exists in the $MAC_FILE
if [[ `grep -wi $client_mac $MAC_FILE | wc -l` -lt "1" ]]
then
	if [ "$sendemail" = 1 ]
	then
		do_send_email $client_name $client_ip $client_mac $email_to $email_from
	fi
	if [ "$sendprowl" = 1 ]
	then
		/usr/bin/curl https://prowl.weks.net/publicapi/add -F apikey=$apikey -F application="dhcpd" -F priority=0 -F event="Network Device Added" -F description="$(date): Device Added on Network: Hostname: $client_name IP: $client_ip MAC: $client_mac"
	fi
	#Finally add the $client_name and $client_mac to the $MAC_FILE
	echo "$client_name $client_mac" >> $MAC_FILE
fi
exit 0;

 


Viewing all articles
Browse latest Browse all 60861

Trending Articles