Script para Enviar IP vía Mail

d
Autor: @Desconocido
Descripción: Este script comprueba la dirección IP pública de un PC y la envia a una dirección de correo.
Es útil si tiene el PC con una conexión a internet sin IP fija y se quiere conectar a él remotamente.
Puede ejecutarlo directamente con: ./mailip.sh o añadir la siguiente linea en un script de inicio: nohup mailip.sh &>/dev/null & Para ejecutarlo, primero debe modificar las variables del principio del script con sus datos de correo.
#!/bin/bash

# mailip.sh

# Script designed to read your public IP address from www.whatismyip.org
# at intervals and notify any changes by sending an email for you.

# wget and sendEmail with tsl support must be installed on your system.

# Usage:  mailip.sh  (if it can be found in your session environment PATH)
# or if you want to run it on the background:  nohup mailip.sh &>/dev/null &

# Please, you must modify the next variables to suit your needs:

DELIVER=an_account@gmail.com  # An account to send the email with the ip.
RECEIVE=$DELIVER  # An account to get the email. Don't modify to send it to the same address you especified above.
SMTP_SERVER=server.address.com:port  # The smtp server's port of the emailer account. Example: smtp.gmail.com:587
MAIL_USER=account-user
MAIL_PASS=UseR.p4s5vooRd-
TLS=yes  # yes|no    Does the mail server accept/require TLS or SSL encryption?

# The script starts here.

PATH=$PATH:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin  # paranoia
READ_IP()
{
wget -q -t 4 --no-proxy --no-cache --no-cookies -o /dev/null -O /var/publicip http://www.whatismyip.org
if [ $? -ne 0 ] ; then sleep 5m ; READ_IP ; fi
}
SEND_IP()
{
sendEmail -q -f $DELIVER -t $RECEIVE -u $SUBJECT -m $MESSAGE -s $SMTP_SERVER -xu $MAIL_USER -xp $MAIL_PASS -o tls=$TLS
if [ $? -ne 0 ] ; then sleep 5m ; SEND_IP ; fi
}

if [ $DELIVER == an_account@gmail.com ]
then echo ; echo "  Warning!  You must carefully edit some variables of the script first of all." ; echo
else
  if [ ! -e /var/publicip-sent ]
  then echo 0 > /var/publicip-sent
  fi
  while true
  do READ_IP ; sleep 12s ; sync  # paranoia
    if [ `cat /var/publicip` != `cat /var/publicip-sent` ]
    then
    SUBJECT=`hostname ; echo "IP changed:" ; cat /var/publicip`
    MESSAGE=`cat /var/publicip ; echo ; date +%F\ %T`
    SEND_IP && mv /var/publicip /var/publicip-sent
    else rm /var/publicip
    fi
  sleep 60m
  done
fi

# That was all.

# Perhaps you didn't like having to type your email password directly in the script,
# a workaround could be to assign a value like MAIL_PASS=`cat ~/password.txt` , then
# write a file called ~/password.txt with only the password in it and run the script.
# Don't forget that the password plain text file should only be readable by you,
# modificate it's permissions with chmod if needed.

2 comentarios:

Demian Freeak dijo...

Hola, muy buen blog! revisa la linea 44 demaciados argumentos
./mailip.sh: line 44: [: too many arguments

afcuba dijo...

Buen día, a mi me da el mismo error en la línea 44, al revisar paso a paso el script lo que ocurre es que no genera el fichero publicip