Sincronizacion De Fecha Hora En Maquinas Virtuales Con Proxy Http

situacion:

maquina virtual con centos y encaminada por un proxy http en puerto 3128 con autencticacion

el servicio ntp no puede sincronizarse con el exterior, y se desfaza por una situacion de calculo de la maquina virtual

se carga este script en un area cron pra que se descargue periodicamente un html y parsee el contenido para luego setear la fecha

#!/bin/bash
# 2010 (c) deshn, kseltar
# date/hwclock sinchronizer : on proxy http virtualized enviroment
 
# This software is under GPLv3 or newer
 
# se indica la la uri donde se puede enconntar la hora exacta
timeuri="http://www.timeanddate.com/worldclock/city.html?n=21";
 
# el script a continuacion se especializa en el contenido del la uri timeanddate.com 
 
# se descarga el contenido
wget -c $timeuri -O /tmp/asutimenow0
 
# se indican textos q conrrepondan a la linea q se requiere (esta salida contiene lineas largas) 
opentag="Current Time";
closetag="Show clock";
 
# encontrar el texto que aparece a principio del parrafo (cerca del dato deseado)
grep "$opentag" /tmp/asutimenow0 > /tmp/asutimenow1
 
#linea actual
lnum=0
 
#analizar cada linea
while read -r line
do
    #eliminar todo hasta el parrado donde aparece el texto inicial
    line=${line#*$opentag}
    #eliminar todo desde el parrado donde aparece el texto final
    line=${line%$closetag*}
    #salta de la primera aparicion
    if (( lnum )); then
        echo "$lnum $line s"
    fi
    (( lnum ++ ))
done < /tmp/asutimenow1
 
#leer los datos capturados y limpiar de caracteres indeseados
wheretime=`cat "/tmp/asutimenow1"`;
 
wheretime=${wheretime// /_};
wheretime=${wheretime//"\""/_};
wheretime=${wheretime//</_};
wheretime=${wheretime//>/_};
wheretime=${wheretime//=/_};
wheretime=${wheretime//\//_};
wheretime=${wheretime//:/_};
wheretime=${wheretime//&/_};
wheretime=${wheretime//./_};
wheretime=${wheretime//,/_};
wheretime=${wheretime//;/_};
wheretime=${wheretime//\?/_};
wheretime=${wheretime//\%/_};
wheretime=${wheretime//|/_};
 
#eliminar texto indeseado
wheretime=${wheretime#___*Current_Time};
wheretime=${wheretime/__th__td__strong_id_ct__class_big_/};
wheretime=${wheretime%__strong__br_Show_clock*PYT__td___tr_};
wheretime=${wheretime#*__};
 
#extraer datos de fecha-hora
nwday=${wheretime%%_*};
wheretime=${wheretime#*_};
nwmonth=${wheretime%%_*};
wheretime=${wheretime#*_};
nwyear=${wheretime%%_*};
wheretime=${wheretime#*__};
nwhour=${wheretime%%_*};
wheretime=${wheretime#*_};
nwminute=${wheretime%%_*};
wheretime=${wheretime#*_};
nwsecond=${wheretime%%_*};
wheretime=${wheretime#*_};
 
#eliminar archivos temporales
rm -f /tmp/asutimenow*
 
if [ "$nwmonth" == "January" ]; then echo nwmonths=01; fi
if [ "$nwmonth" == "February" ]; then echo nwmonths=02; fi
if [ "$nwmonth" == "March" ]; then nwmonths=03; fi
if [ "$nwmonth" == "April" ]; then nwmonths=04; fi
if [ "$nwmonth" == "May" ]; then nwmonths=05; fi
if [ "$nwmonth" == "June" ]; then nwmonths=06; fi
if [ "$nwmonth" == "July" ]; then nwmonths=07; fi
if [ "$nwmonth" == "August" ]; then nwmonths=08; fi
if [ "$nwmonth" == "September" ]; then nwmonths=09; fi
if [ "$nwmonth" == "October" ]; then nwmonths=10; fi
if [ "$nwmonth" == "November" ]; then nwmonths=11; fi
if [ "$nwmonth" == "December" ]; then nwmonths=12; fi
 
echo "$nwyear $nwmonths $nwday $nwhour $nwminute $nwsecond";
 
echo
#echo $line;
fecha="$nwyear-$nwmonths-$nwday $nwhour:$nwminute:$nwsecond";
/bin/date --set --date="$fecha"
/sbin/hwclock --set --date="$fecha"
echo "hora seteada";
 
#fuente = http://tldp.org/LDP/abs/html/ 
#fuente = http://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-replace-special-characters-in-a-string-within-a-bash-script-646668/
#fuente = http://www.cyberciti.biz/faq/howto-set-date-time-from-linux-command-prompt/
#fuente = http://www.humbug.in/aside/bash-tricks-split-cut-a-string-with-multi-character-delimiters/
#fuente = http://www.linux-party.com/modules.php?name=News&file=article&sid=1732/cambiar-la-hora-y-la-fecha-al-sistema-linux
#fuente =  http://www.murga-linux.com/puppy/viewtopic.php?p=437921&sid=ad5f3733e33ee9cd6a19633ee8b69cc9
#fuente = http://www.taringa.net/posts/linux/4112201/Cambiar-fecha-y-hora-en-mi-Linux_Administrador.html
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.