You are not logged in.
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
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
hmm on my system that doesn't produce any output...
Offline
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
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
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
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
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
i see.
Offline
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
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
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
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
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
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline