LINUX - APP / SCRIPT - Public IP Change Notification to SMS and Email

Public IP Change

For: Any Linux (Tested on Debian 6)

=== Some random info ===

Lots of people alot of money to get static IP address. There are alot of benefits to having the same Static IP. If you have your IP tied to a Domain, and it changes, even if you update it, it could take up to 24 (or 48 hours) to update through out the DNS servers. This could be very hazardous to business etc. The best solution is definatly having a none changing ip. Some of us arent that lucky though. So the solution is to have your system update you when the public IP changes.

I have two methods that keep updating me. The second way is optional. 

The first way checks if the IP changes and emails me (I have it email to my phone via sms text message and my email)

The second optional way is that I just have my system update me of my IP 3 times everyday via email (again to my phone via sms text message and my email)

I use the sms gateway email of my phone service provider to send an email, it then forwards a text message to my phone. I will provide a list of the phone service providers different sms & mms gateways later on.

=== Tools I use ===

For the operations of the code I just use BASH script. For scheduling I use cron jobs. For my system to email me, I use postfix.


=== Step 1 : Postfix - System Emails ===

# apt-get install postfix

When asked for what kind of email server select "Internet Site"
Leave everything at default

Test the email like so
First type "# mail"
Then in the subject put "TEST" and hit enter
Then type the email body "This is a test message" and hit enter and type a dot/period ".". Te period is a EOT character. EOT meaning End of Transmitions notifies the system that the email is complete.

# mail
subject: TEST <enter>
This is a test message <enter>
. <enter>

=== Step 2 : Make the apps location ===

First log in as root
And go to roots home directory

# cd ~
NOTE: in my examples this is /root/

Make the ipchange folder where the apps will go and inside it the log folder where the logs will go

# mkdir -p ipchange/logs
NOTE: in my examples this is /root/ipchange and /root/ipchange/logs
# cd ipchange
=== Step 3a : Write the 2 scripts ===
I use vim (when inside it hit "i" to start typing, and then "esc" to stop typing, once out of the typing mode, aka insert mode, type ":wq!" to save and exit"). Then I verify the scripts with cat command.

If you dont have vim type # apt-get install vim

Make the file and give it the proper execution attributes.

# touch
# chmod +x

# touch
# chmod +x

Now that the scripts are made and can be execute, its time to write the instructions into them.
=== Step 3b : Write the script ===

This script will send the new ip by email if it has changed. It gets the IP and saves it to a file, next time it runs it checks to see if that file has the same or different value. If the value is different, then it must be a new IP so it emails it.

# vim

Type "i" to enter insert mode
Type the following (in between the tidle break lines, obviously dont include the tidle break lines):
Note: change the EMAIL1, EMAIL2, and EMAIL3 to 3 different emails. I have EMAIL3 send it to my phone where 5551234321 is my phone number and is the text gateway of my phoneprovider (I have a list of the phone gateways at the bottom)


# this one mails you the ip if it changes


rm -f index.html temp
touch old_ip.dat

wget > /dev/null 2> /dev/null

read T1 < index.html
read T2 < old_ip.dat

if [ "$T1" = "$T2" ]; then
    echo "`date` -- ipchange script -- IP is the same: $T1 - doing nothing " | tee -a /var/log/syslog
# ANOTHER WAY: (echo "From:"; echo "To:"; echo "Subject: IP Address Update"; echo; echo "Computer's IP Changed. The new IP is:"; cat index.html) | sendmail -f
    echo "`date` -- ipchange script -- THE NEW IP IS: $T1" > temp
    cat temp >> /var/log/syslog
    mail -s "IP CHANGED" $EMAIL1  < temp
    mail -s "IP CHANGED" $EMAIL2  < temp
    mail -s "IP CHANGED" $EMAIL3  < temp
rm -f old_ip.dat temp
mv index.html old_ip.dat


Type "Escape" to quit insert mode
Then type ":wq!" to force save anf quit.

=== Step 3c : Write the script ===

This script emails the current IP to the email.
# vim
Type "i" to enter insert mode
Type the following (in between the tidle break lines, obviously dont include the tidle break lines):

Note: change the EMAIL1, EMAIL2, and EMAIL3 to 3 different emails. I have EMAIL3 send it to my phone where 5551234321 is my phone number and is the text gateway of my phoneprovider (I have a list of the phone gateways at the bottom)


IP=`curl 2> /dev/null`
FILE=`date +D%D-T%T | tr / - | tr : -`
mkdir $FOLDER/logs 2> /dev/null
echo $TARGET "<--TARGET"
echo $FILE": THE IP IS:" $IP | tee $TARGET
mail -s "IP LOG: $DATE" $EMAIL1 < $TARGET
mail -s "IP LOG: $DATE" $EMAIL2 < $TARGET
mail -s "IP LOG: $DATE" $EMAIL3 < $TARGET


Type "Escape" to quit insert mode
Then type ":wq!" to force save anf quit.

=== Step 4 : Setup the Cronjob ===

In linux cron jobs run scheduled tasks.

Every user has cron jobs. Google "cron examples" to learn more. For now we will make a cronjob that runs as root, these run in the background.

To see all the current root cron tasks type # crontab -l. Dont be surprised if you have none. There are actually more cronjobs that just that. The other ones usually go to /etc/cron.d

To edit crontabs run the command below, it might ask what editor to use, Just use vim or vi, or nano/pico. If you use nano/pico (Save is: Control-O then enter enter, and Quiting is Control-X)

# crontab -e

Add the following lines into it(at the bottom) and save and exit:

NOTE how you can use # hash symbols in crontabs to comment stuff out

# this logs the ip and emails it (twice a day)
# the @midnight is a shortcut for midnight, so that the iplog sends you your IP at midnight of every day
# the 0 12,18 * * * sends updates at noon and 6pm
@midnight /root/ipchange/
0 12,18 * * * /root/ipchange/

# this looks for email changes every 5 minutes
# If it were all start * * * * * it would check every minute but */5 makes it skip 5 minutes

*/5 * * * * /root/ipchange/


=== THE END ====

Final folder structure in my examples is like so

/root/ipchange/logs/<logs will go here>


Now all this should just run

But just incase reload the cron job

# /etc/init.d/cron reload

=== List of other sms gateways ===

I should put this into a table but oh well (google for" sms gateway" or "mms gateways")
Side Note: For MySimpleMobile (My Simple Mobile) MSM - Tmobile gateway works for example: will send sms text 5551112222 mysimplemobile phone.

Aircel     India     Not working for some reason         
Airtel     India     Not working for some reason         
Airtel     Karnataka, India     Not working for some reason         
Ahra Samaneh Iranian Co. ( ???? ?????? ???????? )     Iran     (missing?)     [1]         
Airfire Mobile     USA     [2]         
Alaska Communications     USA     [3]         
Aliant     Canada              
Alltel (Allied Wireless)     USA (SMS) (MMS)
Verizon Wireless(merger with Alltel was completed in November 2009)[1]     USA[2] (SMS & MMS) (SMS) (MMS)[4]         
Altiria     Spain[3]     SMS masivo         
Ameritech     USA     [5]         
Amitas Digital     Australia          [6]         
Andhra Pradesh AirTel     India     [7]         
Andhra Pradesh Idea Cellular     India     [8]         
Api4SMS     Austria     Requires Login SMS Gateway         
aql     UK[4]     [9]     44 7766 40 41 42     address text
SourceSMS     UK          [10]     address text    
AT&T Wireless     USA (SMS), (MMS) (Note: This gateway fails to handle complete phone numbers; the country code must be omitted, leaving only the ten-digit NANP number.)[5]
Requires login[6]     121 111[7]     address text
address (subject) text
AT&T grandfathered customers (originally AT&T, then Cingular, now AT&T Wireless). Officially offline since June 10, 2010, under AT&T ticket CM20100610_10186966.     USA          121     address text
AT&T Mobility (formerly Cingular)     USA
AT&T Enterprise Paging     USA              
AT&T Global Smart Messaging Suite - Powered By Soprano     USA     [8]         
AU by KDDI     Japan     [11]         
Bell Mobility & Solo Mobile     Canada or [12]         
BellSouth     USA     [13]         
blablaSMS     International          [14]         
Bluegrass Cellular     USA (SMS) (MMS) [15]         
Bluesky Communications     American Samoa, USA              
Boost Mobile     USA              
Bouygues Telecom (company)     France              
Box Internet Services SMS Gateway     Switzerland (SMS) (MMS)
SMS Gateway + SMS API [9]
SFR     France          International          Requires Login SMS Gateway     International     Requires Login SMS Gateway API     International     Requires Login [16]         
C Beyond (All Page Wireless)              
Cellcom     USA              
Cellular South     USA              
Centennial Wireless     USA, Puerto Rico, and the U.S. Virgin Islands (merging with AT&T Wireless)     [17]         
Chariton Valley Wireless     USA              
Chennai Skycell / Airtel     India     [18]         
Chennai RPG Cellular     India     [19]         
China Mobile     China              
Cincinnati Bell     Cincinnati, Ohio, USA (SMS) (MMS)
Cingular (Postpaid)     USA [20] (must be logged in prior to visiting this link)         
Cingular (GoPhone prepaid)     USA (SMS)     Not working-GoPhone is now AT&T         
Claro     Brazil     [21] (registration not required)         
Claro     Nicaragua     [22]         
Claro     Peru          [23]         
Claro     Puerto Rico     [24]         
Cleartalk     USA     [25]         
Claro     Colombia     [26]         
Connection Software (CSoft)     United Kingdom     [27]          Subject and Body are concatenated into the text message
Cricket     USA (SMS) (MMS) [28]         
C Spire Wireless     USA     [29]         
Accessyou     Hong Kong (MMS)     [30]         
CSL     Hong Kong (MMS)     [31]         
CTI Móvil (Now Claro)     Argentina     [32]         
CTI Móvil (Now Claro)     Paraguay          [33]         
CTI Móvil (Now Claro)     Uruguay          [34]         
Delhi Airtel     India     [35]         
Delhi Hutch     India     [36]         
Digicel (Dominica)     Dominica     (no-area-code)     [37]         
E-Plus     Germany     [38]         
Edge Wireless     USA     [39]         
Element Mobile     USA              
Emtel     Mauritius     [40]         
Esendex     UK     [41]         
Esendex     USA     [42]         
Esendex     Spain     [43]         
Eventis     Moldova          [44]         
Fido     Canada (as of June 2012)     [45] ( Rogers' Web server is faster )         
Firmensms     Austria     Requires Login SMS Gateway         
Freebie SMS     Europe     [46] ( Message in subject line )         
General Communications Inc.     Alaska, USA     [47]         
Globalstar (satellite)     International     [48]         
Globul     Bulgaria     [49]         
Goa Airtel     India     [50]         
Goa BPL Mobile     India     [51]         
Goa Idea Cellular     India     [52]         
Golden State Cellular     California, USA     [53]         
Gujarat Idea Cellular     India     [54]         
Gujarat Airtel     India     [55]         
Gujarat Celforce / Fascel     India     [56]         
Haryana Airtel     India     [57]         
Haryana Escotel     India     [58]         
Hawaiian Telcom Wireless     Hawaii, USA              
Hay Systems Ltd (HSL)     UK[10]     [59]         
Helio     South Korea and USA              
Himachai Pradesh Airtel     India              
ICE     Costa Rica          1001     address : (subject) text
Iridium (satellite)     International[11]     [60]         
i wireless (T-Mobile)     Midwest, USA     [61]         
i-wireless (Sprint PCS)     USA     [62]         
Kajeet     USA              
Karnataka Airtel     India     [63]         
Kerala Airtel     India     [64]         
Kerala BPL Mobile     India     [65]         
Kerala Escotel     India     [66]         
Kolkata Airtel     India     [67]         
Koodo Mobile     Canada     [68]         
LongLines     USA     [69]         
Loop (BPL Mobile)     Mumbai, India              
Lynx Mobility     Canada     [70]         
M1     Singapore     [71]         
Madhya Pradesh Airtel     India     [72]         
Maharashtra Airtel     India     [73]         
Maharashtra BPL Mobile     India     [74]         
Maharashtra Idea Cellular     India     [75]         
Más Móvil     Panama     [76]         
Mediaburst     UK     [77]         
Ncell(Previously Mero Mobile)[12]     Nepal (Requires registration)     [78]         
Meteor     Ireland (SMS) (Not currently valid) (MMS) [79]         
MetroPCS     USA              
Moldcell     Moldova          [80]         
Mobiltel     Bulgaria     [81]         
Mobitel     Sri Lanka     [82] Mobitel no longer offers email2sms.         
Movistar     Argentina     [83]         
Movistar     Colombia     [84]         
Movistar     Spain     [85]         
Movistar     Spain and Latin America              
Movistar     Uruguay              
MTN     South Africa     [86]         
MTS Mobility     Canada     [87]         
Mumbai Airtel     India     [88]         
Mumbai BPL Mobile     India     [89]         
My-Cool-SMS     United Kingdom     [90]         
Nepal Telecom     Nepal          [91]         
Nextech     USA     [92]         
Nextel     USA     [93]         
Nextel     Mexico     [94]         
Nextel     Argentina     [95]         
NTT DoCoMo     Japan     [96]         
O2     Germany     [97]         
OgVodafone     Iceland     [98]         
Orange Moldova     Moldova          [99]         
Orange     Netherlands     [100]         
Orange     Switzerland          [101]         
Orange     United Kingdom     [102]         
Page Plus Cellular (Verizon MVNO)     USA (SMS) (MMS) [103]         
Panacea Mobile     Worldwide (SMS)     [104] (requires login)          subject: username password
body: the message
Pioneer Cellular     USA              
Personal     Argentina (call for activation)     [105] [106]         
Plus     Poland (     [107]         
Pocket Wireless     Texas, USA              
Pondicherry BPL Mobile     India     [108]         
PC Telecom     Canada              
Punjab Airtel     India     [109]         
Qwest Wireless     USA     [110]         
Red Pocket Mobile (AT&T MVNO)     USA (SMS) (MMS)
Rogers Wireless     Canada and USAlog or     Requires Login [111]          Name (subject) text (Note: Subject must be in plain text, not Q-encoded as described in RFC 2047.)
RoutoMessaging     Worldwide     [112]          Subject: Sender ID (Originating Address who is sending the message) Body: the message (Note: plain text emails supported)
SaskTel     Canada [113]         
Sendega     Norway (registration required)     [114]          Content support: subject, body or subject + body. Settings available from website (login required)
Setar Mobile email (Aruba)     Aruba (     [115]         
Siminn     Iceland     [116]         
Simple Mobile     USA     [117]         
SMS Broadcast     Australia     [118]         
SMS Central     Australia     Requires Login [119]         
SMS4Free     USA          [120]         
(Earn points by participation in marketing/promotions)
Australia     Requires Login [121]         
Softbank     Japan     N/A[13]         
Starhub Enterprise Messaging Solution (powered by Soprano)     Singapore )     [122]         
Southernlinc     USA (SMS)              
South Central Communications     USA (SMS)     [123]         
Spikko     Israel     [124]         
Sprint (PCS)     USA (SMS) (MMS)
[125] requires login     6245     address message
Nextel (Sprint)     USA (Rich Messaging) (SMS) [126]
requires login
Straight Talk     USA (SMS) (SMS if using the AT&T SIM) (MMS or SMS when using the T-Mobile SIM, begin 'number' with 1 then phone number) (MMS) (MMS & SMS) (T-Mobile SIM MMS & SMS)
Sunrise Communications     Switzerland     [127] (registration required)         
Swisscom     Switzerland          [128] [129] [130]
Synergy Communications     South Africa     [131]         
Syringa Wireless     USA              
TeletopiaSMS     Norway (registration required)     [132]          Content settings available from website
T-Mobile     USA (MMS or SMS) --number can and by default properly begins with "1" (the US country code)     [133]     500[14]     address text
T-Mobile (Optus Zoo)     Australia     [134]         
T-Mobile     Austria (     [135]         
T-Mobile     Croatia (     [136]     100     address##subject#text
T-Mobile     Germany     [137]         
T-Mobile     Netherlands     [138]         
Tamil Nadu Airtel     India     [139]         
Tamil Nadu Aircel     India     [140]         
Tamil Nadu BPL Mobile     India     [141]         
Tele2     Sweden     [142]         
Telecom New Zealand     New Zealand     [143]         
Teleflip     USA     [144]         
Telekom Srbija     Serbia          [145]         
CS Networks     Serbia     [146]         
Telstra Integrated Messaging (powered by Soprano)     Australia     [147]         
All Australian Mobile Networks Free 10 SMS to try (powered by IT Company)     Australia     [148]         
Telus Mobility     Canada and USA (SMS) (MMS) [149]         
TellusTalk     Europe     [150]         
TextOver     UK     Log in to TextOver for details     [151]         
TextAnywhere     UK          [152]         
Thuraya (satellite)     International          [153]         
Tigo (Formerly Ola)     Colombia     [154]         
TIM     Italy     [155]         
TracFone (prepaid)     USA, Puerto Rico, and the U.S. Virgin Islands     direct:
Txtlocal     UK     [156]         
UkraineCalling     International          [157]         
Unicel     USA     [158]         
UniMóvil Corporation     UK     [159]         
Unité     Moldova          [160]         
US Cellular     USA (SMS) (MMS) [161]         
USA Mobility     USA (SMS)     [162]         
UTBox     Australia     [163]         
Uttar Pradesh West Escotel     India     [164]         
Verizon Wireless     USA (SMS) (MMS) [165]     6245     address (subject) text
Viaero     USA (SMS) (MMS) [166]         
Vivo     Brazil     [167] (registration required)         
Virgin Mobile     Canada              
Virgin Mobile     USA (SMS)[15] (MMS)[16]
Virgin Mobile     United Kingdom     Does not appear to work [168]         
Vodacom     South Africa     [169] (registration required)         
Vodafone     Germany     [170]         
Vodafone     Italy     3**              
Vodafone     International          [171] (registration required)         
Vodafone     Chuugoku/Western, Japan     [172]         
Vodafone     Hokkaido, Japan     [173]         
Vodafone     Hokuriku/Central North, Japan     [174]         
Vodafone     Kansai/West, including Osaka, Japan     [175]         
Vodafone     Kanto/Koushin/East, including Tokyo, Japan     [176]         
Vodafone     Kyuushu/Okinawa Japan     [177]         
Vodafone     Skikoku, Japan     [178]         
Vodafone     Touhoku/Niigata/North, Japan     [179]         
Vodafone     Spain     [180]         
Vodafone     New Zealand     [181]         
Voyager Mobile     United States              
West Central Wireless     USA     [182]         
Willcom     Japan (di) (dj) (dk) [183]         
Wind Mobile     Canada          4000 [17]     address text
XIT Communications     USA - Texas     [184]         
Polkomtel     Poland     [185]