Hi, I will try to brief as I know some of you will probably see what im trying to do..

What I am trying to do is have the IP address of the machine compare its IP to the IP that is on the server. I am trying to get it to read the first two octets (10.122.x.x). If the first two octets are different then that of the IP on the server I would like it to send a email to notify that the computers IP has been changed. The reason for this is to keep track of the computer. We have several schools and each school is distinguished by the second octet ("school 1" 10.111.x.x, "school 2" 10.122.x.x". I have the naming of the computer and the email set, I am just stuck on it comparing the first to octets.. Any help is appreciated.


Script:

#! /bin/sh


PATH=/bin:/sbin/:/usr/bin/:/usr/sbin

HWADDRESS=`ifconfig en0 | grep ether | awk '{print $2}'`
IPADDRESS=`ifconfig en0 | grep inet\ | awk '{print $2}'`
SUBNET=`echo $IPADDRESS | awk '{print $2}'`
WEBSERVER='http://10.108.1.227/computer_log/'
NAMEFILE='namelist.txt'
WEBNAMELIST=$WEBSERVER$NAMEFILE
NAMELIST=`curl -fRs -o /tmp/namelist $WEBNAMELIST`
NAMELIST_RESULTS=`grep $HWADDRESS /tmp/namelist | awk '{print $2}'`
IPLIST_RESULTS=`grep $IPADDRESS /tmp/namelist | awk '{print $2}'`
MATCH=`echo $IPLIST_RESULTS | awk '{print $2}'`

###### SET COMPUTERNAME VARIABLE TO CURRENT NAME IF NO VALUE IN NAMELIST OR VALUE IN NAMELIST ######
if [ "$NAMELIST_RESULTS" = "" ]; then
COMPUTERNAME=""
else
COMPUTERNAME=$NAMELIST_RESULTS
fi

###### IF COMPUTERNAME IS BLANK, SET NAME TO HWADDRESS, OTHERWISE SET TO NAMELIST VALUE ######
if [ "$COMPUTERNAME" = "" ]; then
NONAME=`echo $HWADDRESS | awk '{ gsub(":", ""); print $0}'`
ncutil setprop / computer-name $NONAME
ncutil -activate setprop / local-host-name $NONAME
else
ncutil setprop / computer-name $COMPUTERNAME
ncutil -activate setprop / local-host-name $COMPUTERNAME
fi

##### CHECK IP ADDRESS!, AND SEND EMAIL IF COMPUTER IS NOT IN IP RANGE #####

if [ "$SUBNET" = "$MATCH" ]; then
exit
else

sudo postfix start

echo "Computer $COMPUTERNAME has been moved!! The IP address of this machine is now $IPADDRESS" | mail -s "Computer-$COMPUTERNAME has been moved" email@email.org


fi

exit

What I am trying to do is compare the first two octets from the current ip to that of the ip that is in a .txt file on the server.. I am getting it to see the current ip address with $IPADDRESS i just need to compare to the ip address on $IPLIST_RESULTS.. I only need it to compare the first two octets as this will show that it has been moved to a different location. Thanks and once again any help is greatly appreciated..

This article has been dead for over six months. Start a new discussion instead.