msgbartop
A chronological documentation test project, nothing serious, really!
msgbarbottom

21 Jan 2009 IP address change notifier script

This is a simple bash script that is run by crontab every 5 minutes on a linux box.
It e-mails me the new address when a change of IP address is detected.

The script (ipchangemail.sh)

#!/bin/bash 

# Check if IP-address has changed. If a change has occured, mail me the new address
# Add the following line to crontab if you would like it to be run every 5 minutes:
# */5 * * * * ./ipchangemail.sh

# The network interface I want to monitor
NET_INTERFACE=eth0

# File to keep the latest IP address
IP_FILE=myip.txt

# Mail to this address when a change occur
MAILTO=mail@example.com


# Read the previous IP address from file
source $IP_FILE

CURRENT_IP=`/sbin/ifconfig $NET_INTERFACE | sed -n "/inet addr:.*255.255.25[0-5].[0-9]/{s/.*inet addr://; s/ .*//; p}"`

if [ "$CURRENT_IP" != "$OLD_IP" ]
then
        # Send email about address change
        `echo "New IP address detected: $CURRENT_IP" | mail -s "New IP address" $MAILTO`

        # Write new address to file
        `echo "OLD_IP=$CURRENT_IP" > $IP_FILE`
fi

The script can be downloaded here.

Tags: , , ,

Posted by

30 Nov 2007 Create virtual / alias IP address

This is the code to make an alias IP address on network interface ethX where X is a number to indicate the device we are attaching the IP.
ifconfig eth0:1 192.168.0.30 netmask 255.255.255.0 up
Your machine will now answer on ping requests 192.168.0.30.

You can see the result by running the command

ifconfig eth0:1

Result

eth0:1    Link encap:Ethernet  HWaddr xx:xx:xx:xx:xx:xx
          inet addr:192.168.0.30  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          Interrupt:17 Base address:0x1080

To take down this alias use

ifup eth0:1 down

You might experience that the new device/address won’t answer network requests to the IP address after moving it. This happens because the arp cache on the router most likely haven’t discovered that the IP address is on a new MAC address. A solution to this problem is to clear the arp info on the router using the arprelease command

arprelease eth0:1 192.168.0.30

This command can be downloaded from http://sourceforge.net/projects/arprelease/ if it isn’t available on your linux distribution.

Tags: , , ,

Posted by

20 May 2007 Get your IP address

ifconfig eth0 | awk '/inet addr/{print $2}' | cut -d: -f2

Tags: , ,

Posted by