You are not logged in.

#1 2012-06-29 13:39:46

train_wreck
Member
Registered: 2011-10-22
Posts: 97

my super simple ip checking script

i have a small home network with a PC running arch that acts as a NAT router; in the near future i am planning on setting up VPN access on this arch box. i would assume that it is imperative to know the external IP address of the router in order to connect. my ISP is comcast (cable) and they seem to be rather active in changing my DHCP'd address i get from them (in the past 6 months i have had 3 different ones). so i wrote this bash script that runs in the background and monitors the IP of the external interface. its pretty self explanatory; the mails to "vtext.com" are so i can have it texted to me. pointers would be welcome. i am not particularly a programmer, much more just an admin. the awk stuff seems messy, like there should be an easier way to isolate strings than that.... but idk. i run it

#!/bin/bash

# IP CHECK 2.0 (file name/path: /etc/scripts/ipcheck)
# runs in the background and 
# requires sleep, mailx & ifconfig
# assumes external interface named "eth0"


# current ip output file path - single-line file containing current address (needs to be writable)

OPATH="/var/ipcheck/currentip"

# Amount of seconds to wait between checks

WAITSECS=300 # 5 mins

# Read previous IP from specified $OPATH file, if it's there

if [ -f "$OPATH" ]; then 
	OLDIP=`cat $OPATH`; 
fi

# Begin main program loop

while :
do

	# retrieve new ip using ifconfig
	
	NEWIP=`/sbin/ifconfig eth0 | awk 'BEGIN{FS="inet ";RS=" netmask"}/</{print $2}' | tr -d '\\n' | tr -d ' '`
	
	# print whatever was awk'd and the current date/time to tty11

	echo "=========   $NEWIP  --  `date`" >> /dev/tty11

	# if a different NEW IP is detected AND it isn't a null value, then do the following:
	# 	* write the new address out to the OPATH file, replacing the previous value
	#	* e-mail the new address to my phone via vtext.com
	#	* e-mail both the old and new addresses to my main e-mail account
	#	* reset the $OLDIP value as the new one

	if [[ "$OLDIP" != "$NEWIP" && "$NEWIP" != ""  ]]; then
	        echo $NEWIP > $OPATH
	        echo "=======  New Address - Old IP: $OLDIP ---- New IP: $NEWIP" >> /dev/tty12
        	echo "New IP: $NEWIP" | /usr/bin/mailx 5555555555@vtext.com
	        echo "Old IP: $OLDIP" | /usr/bin/mailx -s "New IP: $NEWIP" myemail@mail.com
		OLDIP=$NEWIP
	fi
	
	# pause in between checks, then start again

	sleep $WAITSECS
done

Offline

#2 2012-06-29 14:13:34

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: my super simple ip checking script

FWIW, I use the following to get IP in my script:

ip -4 a s ethX |awk 'sub(/\/24/, ""){print $2}'

Edit: added closing '

Last edited by alphaniner (2012-06-29 14:16:58)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#3 2012-06-29 14:20:45

train_wreck
Member
Registered: 2011-10-22
Posts: 97

Re: my super simple ip checking script

hmm on my system that doesn't produce any output...

Offline

#4 2012-06-29 14:24:19

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: my super simple ip checking script

You may need to tweak some bits.  What is the output of

ip -4 a s eth0

But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#5 2012-06-29 14:34:36

train_wreck
Member
Registered: 2011-10-22
Posts: 97

Re: my super simple ip checking script

3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 16
    inet 76.22.129.200/23 brd 255.255.255.255 scope global eth0

obviously changing 24 in the 'sub(/\/24/, "")' statement to 23 worked.

Offline

#6 2012-06-29 14:39:45

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: my super simple ip checking script

Yeah, the number after the slash has something to do with your netmask, I think.  I don't fully understand it.  In my case the number is always 24.  It could be changed to [[:digit:]]+ to be more "universal".  Heck, I'll probably go ahead and do that in my script.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#7 2012-06-29 15:19:39

train_wreck
Member
Registered: 2011-10-22
Posts: 97

Re: my super simple ip checking script

indeed, it's referenced here
http://en.wikipedia.org/wiki/CIDR_notation
apparently the number is calculated according to the subnet mask. in my case it's 255.255.254.0

i suppose your method doesn't require ifconfig, and the "ip" program seems to be common everywhere. one more thing, how does yours handle when the interface has lost its IP? (i.e. if the modem is unplugged or just starting up?)

Offline

#8 2012-06-29 15:37:58

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: my super simple ip checking script

The purpose of my script is just to display a summary of each interface:

$ ethall

[eth0]
 10.10.1.2
 100Mb/s
 18.01M / 1.14M

[eth1]
 192.168.1.110
 1000Mb/s
 16.73M / 9.42M

[eth3]
 199.200.1.110
 1000Mb/s
 1.14M / 1.27M

So if the output of ip -4 a s ethX does not contain "inet", the interface is ignored (as was the case for eth2 above).

Last edited by alphaniner (2012-06-29 15:47:53)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#9 2012-06-29 15:39:10

train_wreck
Member
Registered: 2011-10-22
Posts: 97

Re: my super simple ip checking script

i see.

Offline

#10 2012-06-30 02:10:40

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,094

Re: my super simple ip checking script

http://mywiki.wooledge.org/IpAddress

If you want your external ip, just curl one of the 'whatsmyip' style sites instead.


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#11 2012-07-01 00:35:16

Isola
Member
Registered: 2010-02-02
Posts: 99

Re: my super simple ip checking script

Here's my IP checking script, modified for mailx

#!/bin/bash
curl ifconfig.me | read IP

[ "x$IP" == "x`tail -n1 /tmp/wanip`" ] && exit 0

{
   echo    New IP: $IP

   echo -n Old IP:
   cat /tmp/wanip

   echo -n Number of times you had this IP before:
   grep $IP /tmp/wanip | wc -l
} | mailx -s "New IP: $IP" xxxxxx@vtext.com myemail@mail.com

echo $IP >> /tmp/wanip

Offline

#12 2012-07-01 00:45:12

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: my super simple ip checking script

Isola wrote:

Here's my IP checking script, modified for mailx

#!/bin/bash
curl ifconfig.me | read IP

[ "x$IP" == "x`tail -n1 /tmp/wanip`" ] && exit 0

{
   echo    New IP: $IP

   echo -n Old IP:
   cat /tmp/wanip

   echo -n Number of times you had this IP before:
   grep $IP /tmp/wanip | wc -l
} | mailx -s "New IP: $IP" xxxxxx@vtext.com myemail@mail.com

echo $IP >> /tmp/wanip

This can't possibly work as is... The assignment in the first line happens in a subshell. It's going to be blank in the rest of the script.

Offline

#13 2012-07-01 00:55:07

Isola
Member
Registered: 2010-02-02
Posts: 99

Re: my super simple ip checking script

falconindy wrote:
Isola wrote:

Here's my IP checking script, modified for mailx

#!/bin/bash
curl ifconfig.me | read IP

[ "x$IP" == "x`tail -n1 /tmp/wanip`" ] && exit 0

{
   echo    New IP: $IP

   echo -n Old IP:
   cat /tmp/wanip

   echo -n Number of times you had this IP before:
   grep $IP /tmp/wanip | wc -l
} | mailx -s "New IP: $IP" xxxxxx@vtext.com myemail@mail.com

echo $IP >> /tmp/wanip

This can't possibly work as is... The assignment in the first line happens in a subshell. It's going to be blank in the rest of the script.

Okay I admit, I just wrote that without testing it roll

edit:

The 2nd line can be changed for this:

read IP < <(curl ifconfig.me) || exit $?

Or

IP=$(curl ifconfig.me) || exit $?

Last edited by Isola (2012-07-01 00:59:39)

Offline

#14 2012-07-01 00:58:40

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,677
Website

Re: my super simple ip checking script


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

Board footer

Powered by FluxBB