IP Dinámica

atio

 

Para os que temos ip dinámica e queremos montar un servidor, debemos de empregar servizos como os de dyndns, no-ip.com. etc. Seguro que hai centos de servizos deste tipo. Estiven pensando algún xeito para non estar obrigados a empregalos. Ainda que seguro que se pode mellorar moito, a seguinte proposta, pode valer.

Este script envía a nosa ip pública a un servidor ftp. Modificando o script tamén se podería enviar a un servidor ssh.

# This script will optains current external ip
# and save it into a ftp server if change it
#
#
#
#
# This script updated 1/09/2016

HOST='ftp.atio.es'
USER='usuario'
PASSWD='passwd'
FILE='file.txt'
RUTA="http://atio.es/ip/file.txt"

#! /bin/bash
#clear valors
unset last_ip
check_last_ip(){
#wget -q http://atio.es/ip/file.txt -O $FILE
wget -q $RUTA -O $FILE
last_ip=$(cat $FILE)
}
check_current_ip(){
unset tecreset externalip
tecreset=$(tput sgr0)
# Check if connected to Internet or not
ping -c 1 google.es &> /dev/null && echo -e '\E[32m'"Internet: $tecreset Connected" || echo -e '\E[32m'"Internet: $tecreset Disconnected"
# Check External IP
externalip=$(curl -s ipecho.net/plain;echo)
echo $externalip > file.txt
}
#CREDENTIALS TO FTP SERVER
ftp_conection(){
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
cd www/ip
put $FILE
quit
END_SCRIPT
exit 0
}
#COMPARE LAST AND CURRENT IP
compare_ips(){
if [ "$1" == "$2" ]; then
echo -e '\E[32m'"Current IP : $tecreset "$externalip
else
echo "actualizando ip..."
ftp_conection
fi
}
while [ 1 ]; do
  # timestamp
  ts=`date +%T`

  echo "$ts: begin checking..."
  check_last_ip
  check_current_ip
  compare_ips $last_ip $externalip
  sleep 300
done

FICHERO PHP PARA IMPRIMIR EL FILE.TXT EN PANTALLA

<?php
$fh = fopen('file.txt','r');
while ($line = fgets($fh)) {
  // <... Do your work with the line ...>
  echo($line);
}
fclose($fh);
return $line
?>

SCRIPT PARA LEER A IP EXTERNA QUE GARDAMOS NO SERVIDOR FTP
#! /bin/bash
USER=‘usuario’
PASS=‘passwd’
RUTA=”http://atio.es/ip/file.txt
FILE=”file.txt”
wget -q $RUTA -O $FILE
value=$(cat $FILE)
#podemos hacer un escritorio remoto si lo desamos
#rdesktop -z -u $USER -p $PASS -f -k es -r sound:local $value

SCRIPT PARA ACTUALIZAR /ETC/HOSTS

# This script update /etc/hosts
# reading ip value from server
#
#
#
#
# This script updated 1/09/2016

#! /bin/bash
USER=‘usuario’
PASS=‘pass’
RUTA=”http://atio.es/ip/file.txt
FILE=”file.txt”
host_name="fornella.gal"

update_hosts(){
wget -q $RUTA -O $FILE
ip_address=$(cat $FILE)
# find existing instances in the host file and save the line numbers
matches_in_hosts="$(grep -n $host_name /etc/hosts | cut -f1 -d:)"
echo $matches_in_hosts
host_entry="${ip_address} ${host_name}"

if [ ! -z "$matches_in_hosts" ]
then
    echo "Updating existing hosts entry into /etc/hosts."
    # iterate over the line numbers on which matches were found
    while read -r line_number; do
        # replace the text of each line with the desired host entry
        sed -i "/$host_name/ s/.*/$host_entry/g" /etc/hosts
    done <<< "$matches_in_hosts"
else
    echo "Adding new hosts entry into /etc/hosts."
    echo "$host_entry" | sudo tee -a /etc/hosts > /dev/null
fi

}
while [ 1 ]; do
  # timestamp
  ts=`date +%T`
  echo "$ts: begin checking..."
  update_hosts
  sleep 5
done