Laboratory has a server in the room, installed is the Ubuntu Server,ip for automatic allocation, so once the IP changes can not be remote operation, must go to the computer room to record the new IP. Learned a few days after the shell think, is it possible to periodically detect the changes in its IP, once there is a change to send to the mailbox? The first analysis of this task, is nothing more than a few pieces of content:
1, query the current IP;
2, the query before the change of IP;
3, if they do not want to wait, read the mailing list;
4, the list to send mail;
4. Execute the script regularly;
Of course, in the school's online authentication system is a big line of the environment and added another, that is the automatic landing on the Internet Authentication system, which is something.
The first is to query the current IP, will point the Linux children's shoes should be not unfamiliar with ifconfig, input to get:
eth0 Link ENCAP: Ethernet Hardware Address 00:30:48:f9:b7:18
inet Address: 223.2.36.217 Broadcast: 223.2.47.255 Mask: 255.255.240.0
Inet6 Address: fe80::230:48ff:fef9:b718/64 Scope:link
Up broadcast RUNNING multicast mtu:1500 hop count: 1
Receive packet: 439851 Error: 0 Discard: 0 Overload: 0 Number of frames: 0
Send packet: 410241 error: 0 Discard: 0 Overload: 0 Carrier: 0
Collisions: 0 Send Queue Length: 1000
Receive bytes: 76752373 (76.7 MB) Send bytes: 177467784 (177.4 MB)
Interruption: memory:faee0000-faf00000
Lo Link encap: local loopback
inet Address: 127.0.0.1 Mask: 255.0.0.0
Inet6 Address::: 1/128 scope:host
Up LOOPBACK RUNNING mtu:65536 metric: 1
Receive packet: 583701 Error: 0 Discard: 0 Overload: 0 Number of frames: 0
Send packet: 583701 error: 0 Discard: 0 Overload: 0 Carrier: 0
Collisions: 0 Send Queue Length: 0
Receive bytes: 179155467 (179.1 MB) Send bytes: 179155467 (179.1 MB)
But I just want the IP, not interested in a lot of other things, what to do. This requires our grep to come in, grep is the "master" of text lookup, supports regular expressions, and it will return rows of results. We observed that the ip:223.2.36.217 line we needed was preceded by "inet address", so we could grep "inet address". This leaves two lines:
inet Address: 223.2.36.217 Broadcast: 223.2.47.255 Mask: 255.255.240.0
inet Address: 127.0.0.1 Mask: 255.0.0.0
But there are two lines, so we can use the grep-v "127.0.0.1" reverse Select to delete localhost, only our target line is left.
The next step is to get the column, we observe that the target line is separated by: and the space, so we use the next tool, cut, using cut-d:-f2 with: And spaces separated, get the array starting from the second column, this gets the IP. Finally, using awk ' {print '} ' to assign the value to IP, a complete IP acquisition is formed:
ip= ' ifconfig|grep ' inet address ' |grep-v ' 127.0.0.1 ' |cut-d:-f2|awk ' {print '} '
How do you know if there is any change in IP? My approach is to record the IP for the first time, the echo "$IP" >ip_conf,ip_ conf for a file called ip.conf, each time you read this file will know the previous IP, the next is the mailing list, record net-report.conf, to send messages from within them to read, and send mail:
Cat $MAIL _list|while Read mailist
Do
echo "Sending mail to $mailist"
echo "New IP is: $IP" |mail-s newip $mailist
Done
This is basically done, but to say a word, the first to send mail to determine the installation of SendMail or mailutils, and in/etc/host inside change domain name. You receive the email address will be your "user name @ domain name", in addition to external network mailbox, now most have set anti-spam mechanism, tried a bit QQ mailbox and Sina Mailbox, have been intercepted. You want to set up a whitelist to receive mail.
Finally how to execute the shell regularly, use crontab bar, specific to see my reproduced article http://blog.csdn.net/kaixin89/article/details/45199003
The complete script is given below:
#!/bin/bash
IP_CONF='ip.conf'
MAIL_LIST='net-report.conf'
IP=`ifconfig|grep 'inet 地址'|grep -v '127.0.0.1'|cut -d: -f2|awk '{print $1}'`
if [ ! -d $IP_CONF ]
then
touch $IP_CONF
fi
#ip is newest
OLD_IP=`cat $IP_CONF|grep [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*`
echo "IP:$IP OLD_IP:$OLD_IP"
if [ -z "$OLD_IP" ] || [ "$OLD_IP" != "$IP" ]
then
echo "$IP">$IP_CONF
cat $MAIL_LIST|while read mailist
do
echo "Sending mail to $mailist"
echo "New IP is:$IP"|mail -s newIP $mailist
done
fi
To solve the most practical problems in an automated way, although still very food, but a sense of accomplishment ah
Linux under the shell to implement server IP monitoring