Advertisement
Guest User

rc.mailMyIp

a guest
Apr 12th, 2011
1,476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.36 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # /etc/rc.d/rc.mailMyIp
  4. #
  5. # start/check the mailMyIp daemon
  6. #
  7. # To make mailMyIp start automatically at boot, make this
  8. # file executable: chmod 755 /etc/rc.d/rc.mailMyIp
  9. # and add this lines to /etc/rc.d/rc.local
  10. #
  11. # if [ -x /etc/rc.d/rc.mailMyIp ]; then
  12. #  . /etc/rc.d/rc.mailMyIp start
  13. # fi
  14. #
  15. # Written by movebo@linux.ime.usp.br and tested on Slackware 13.1
  16. #
  17.  
  18. ###############
  19. # DEFINITIONS #
  20. ###############
  21.  
  22. FILE=/tmp/.mailMyIP
  23. LOG=/var/log/mailMyIp.log
  24. DATE=`date +"%F %H:%M:%S"`
  25. ADDRS="zeh@gmail.com"
  26. ADDRS2="destino@gmail.com"
  27. MSMTP=`which msmtp`
  28. MSMTPRC=/home/zeh/.msmtprc
  29. PING=`which ping`
  30. SED=`which sed`
  31. PRINTF=`which printf`
  32. IFCONFIG=`which ifconfig`
  33. NAVI_IP=/home/zeh/bin/naviip
  34.  
  35. ####################
  36. # FUNCTIONS: BEGIN #
  37. ####################
  38.  
  39. MSG() {
  40.         BODY="Subject: NAVI has booted, check your IP\nDate: $DATE\n\nNAVI was b
  41. ooted at $DATE\nNAVI IP=$IP\n"
  42.         $PRINTF "%b" "$BODY" > $FILE
  43.         $PRINTF "%b" "$BODY" | $MSMTP -C $MSMTPRC $ADDRS
  44.         $PRINTF "%b" "NAVI has booted at $DATE with IP: $IP\n" >> $LOG
  45. }
  46.  
  47. mailMyIp() {
  48.         if [ -f $FILE ]; then
  49.                 OLDIP=`cat $FILE | sed '/IP=/!d;s/.*=//g'`
  50.                 [ "$IP" != "$OLDIP" ] && MSG
  51.         else
  52.                 MSG
  53.         fi
  54. }
  55.  
  56. OK() {
  57.         echo -e "\\033[60G[  \e[32;1mOK\e[0m  ]"
  58. }
  59.  
  60. FAIL() {
  61.         echo -e "\\033[60G[ \e[31;1mFAIL\e[0m ]"
  62. }
  63.  
  64. GET_IP() {
  65.         IP=`curl -s http://www.whatismyip.com/automation/n09230945.asp`
  66.         echo -n $IP
  67. }
  68.  
  69. TEST_INET() {
  70.         ETH0=`$IFCONFIG eth0 | sed '/inet\ /!d;s/.*r://g;s/\..*$//g'`
  71.         [ -z "$ETH0" ] && ETH=`$IFCONFIG eth1 | sed '/inet\ /!d;s/.*r://g;s/\..*
  72. $//g'` || ETH=$ETH0
  73.  
  74.         if [ $ETH -ne 192 ]; then
  75.                 echo "Network not available"
  76.                 exit -1
  77.         else
  78.                 printf "%b" "Testing your Internet Connection:"
  79.                 PING_COUNT=`$PING -l3 -w1 -c3 200.160.4.2 2> /dev/null | $SED '/
  80. rec/!d;s/.*ted,\ //g;s/\ .*//g'`
  81.         fi
  82. }
  83.  
  84. ##################
  85. # FUNCTIONS: END #
  86. ##################
  87.  
  88.  
  89. case "$1" in
  90. 'start')
  91.         TEST_INET
  92.  
  93.         if [ $PING_COUNT -eq 0 ]; then
  94.                 FAIL
  95.         else
  96.                 OK
  97.                 echo -n "   Retrieving external IP:   "
  98.                 GET_IP
  99.  
  100.                 if [ -n "$IP" ]; then
  101.                         OK
  102.                         echo -en "   Retrieving IP from gmail: "
  103.  
  104.                         if [ -n "$NAVI_IP" ]; then
  105.                                 echo -n "$NAVI_IP"
  106.                                 OK
  107.                         else
  108.                                 FAIL
  109.                         fi
  110.  
  111.                         if [ "$IP" != "$NAVI_IP" ]; then
  112.                                 echo -en "   Mailing the new IP to $ADDRS"
  113.                                 MSG
  114.                                 OK
  115.                         else
  116.                                 echo "   Nothing to do."
  117.                         fi
  118.                 else
  119.                         FAIL
  120.                 fi
  121.         fi
  122.         ;;
  123. 'check')
  124.         TEST_INET > /dev/null
  125.         if [ $PING_COUNT -ne 0 ]; then
  126.                 GET_IP >> /dev/null
  127.                 if [ -n "$IP" ]; then
  128.                         [ "$IP" != "$NAVI_IP" ] && MSG
  129.                 fi
  130.         fi
  131.         ;;
  132. *)
  133.         echo "usage $0 [start|check]"
  134. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement