#!/bin/sh
version=0.2.5
si_log=/home/tmp/si.log
# conf file; cgi_url wol_url
pass_f=/root/.cgi_pass
wn_max=8
auth_wait=20
err_mail() {
err_YmdHM=`LANG=C date '+%Y-%m-%d %a %H:%M'`
err_subj="setiepg.malva.sh error occured on $HOSTNAME at $err_YmdHM"
err_mesg="$err_subj
$1
$2
"
m_server=${m_server:-localhost}
m_user=${m_user:-$LOGNAME}
m_password=${m_password:-.clpasswd}
m_from=${m_from:-$LOGNAME@$HOSTNAME}
m_to=${m_to:-root@$HOSTNAME}
clsmtpc -S -Z smtps -M $m_server \
-A auto -U $m_user -p $m_password \
-F $m_from -T $m_to \
-s "$err_subj" "$err_mesg" ;mv=$?
if [ $mv = 0 ]; then
echo "error mail send, OK" >>$si_log
else
echo "error mail send, FAILED with mv=$mv" >>$si_log
fi
echo "$err_subj" >>$si_log
return $mv
}
set_log() {
if [ -f $si_log ]; then
log_sz=`du -sm $si_log |cut -f1`
if [ $log_sz -gt 2 ]; then
si_date=`date '+%Y%m%d'`
mv $si_log $si_log-$si_date
n=0
for g in $si_log-* ; do
n=$(($n+1))
if [ $n -gt 4 ]; then
rm $g
fi
done
ex_date=`LANG=C date '+%Y-%m-%d %a %H:%M:%S'`
echo "$ex_date :new log start by ${0##*/}-$version " >>$si_log
fi
else
ex_date=`LANG=C date '+%Y-%m-%d %a %H:%M:%S'`
echo "$ex_date :new log start by ${0##*/}-$version " >>$si_log
fi
return 0
}
if [ -f /etc/iepgd.conf ]; then
. /etc/iepgd.conf
fi
server_conf_f=$1
if [ "$2" ]; then
mkepgtab_v=0
else
mkepgtab_v=1
fi
set_log
echo >>$si_log
if [ -f $si_log.auth ]; then
mv $si_log.auth $si_log.auth.pre
fi
if [ ! -f $server_conf_f ]; then
echo "${0#*/} :$server_conf_f is not found" >&2
ex_date=`LANG=C date '+%Y-%m-%d %a %H:%M:%S'`
echo "$ex_date :$server_conf_f is not found" >>$si_log
exit 3
fi
ex_date=`LANG=C date '+%Y-%m-%d %a %H:%M:%S'`
echo "${0##*/}-$version :$ex_date :started" >>$si_log
token_list=""
m=0
while read l ; do
if [ ! "$l" ]; then
continue
fi
cgi_url=`echo $l |cut -d' ' -f1`
test_url=${cgi_url#*://}
cli_hostname=${test_url%%/*}
url_proto=${cgi_url%%:*}
test_url=${url_proto}://${test_url%%/*}
wol_url=`echo $l |cut -d' ' -f2`
i=0
while [ $i -lt 10 ]; do
h_addr=`wget -O - ${test_url}/get_addr.cgi` ;wv=$?
if [ $wv = 0 ]; then
h_addr=${h_addr//$'\r'/}
break
else
wget -O - "$wol_url" >/dev/null 2>&1 ;wv=$?
ex_date=`LANG=C date '+%Y-%m-%d %a %H:%M:%S'`
echo "$ex_date :wget -O - \"$wol_url\" ;wv=$wv :i=$i" >>$si_log
sleep 1m
fi
i=$(($i+1))
done
wn=0
cgi_pass=`cat $pass_f`
while [ $wn -lt $wn_max ]; do
m=$(($m+1))
token=$RANDOM
while [ ${#token} -lt 4 ]; do
token=$token$RANDOM
done
token=`date '+%Y%m%d%H%M'`.$m.${token:0:4}
token_dir=${cgi_url##*xml_dir=}
token_dir=${token_dir%%\&*}
token_dir=${token_dir##*,}
if [ ! "$h_addr" ]||[ $wn != 0 ]; then
h_addr=`wget -O - ${test_url}/get_addr.cgi`
h_addr=${h_addr//$'\r'/}
fi
pass_date=`date '+%Y%m%d%H%M'`
auth=`echo $h_addr$pass_date$cgi_pass |md5sum`
auth=${auth%% *}
echo $cli_hostname:$h_addr:$pass_date:$auth:$m:$wn >>$si_log.auth
fin_url="$cgi_url&token=$token&auth=$auth"
wget_rv=`wget -T $auth_wait -t 1 -O - "$fin_url"` ;wv=$?
ex_date=`LANG=C date '+%Y-%m-%d %a %H:%M:%S'`
echo "$ex_date :wget -O - \"$fin_url\" :wget_rv=$wget_rv :wv=$wv :wn=$wn" >>$si_log
if [ $wv = 0 ]&&[ "$wget_rv" = 0 ]; then
token_list="$token_list ${token_dir%/}/${token}.token"
break
fi
sleep $((${auth_wait}+1))s
wn=$(($wn+1))
done
ex_date=`LANG=C date '+%Y-%m-%d %a %H:%M:%S'`
echo "$ex_date :wget finished ;wv=$wv :wget_rv=$wget_rv :wn=$wn" >>$si_log
if [ $wget_rv != 0 ]; then
nx_err="$ex_date: ${cgi_url%mkts2xml*} ;wv=$wv :wget_rv=$wget_rv :wn=$wn"
if [ "$wget_err" ]; then
wget_err="$wget_err
$nx_err"
else
wget_rv="$nx_err"
fi
fi
done <>$si_log
echo -n "${0##*/} :wait :" >&2
n=0
token_list_check=$token_list
timeout_c=${timeout_c:-240}
while [ $n -lt $timeout_c ]; do
token_list_none=""
for t in $token_list_check ; do
if [ -f $t ];then
ex_date=`LANG=C date '+%Y-%m-%d %a %H:%M:%S'`
echo "$ex_date :${t#*/malva/iepg/} found" >>$si_log
else
if [ "$token_list_none" ]; then
token_list_none="$t $token_list_none"
else
token_list_none=$t
fi
fi
done
if [ "$token_list_none" ]; then
token_list_check=$token_list_none
echo -n "#" >&2
sleep 10s
else
break
fi
n=$(($n+1))
done
err_token=""
for t in $token_list ; do
t_bn=${t##*/}
if [ -f $t ]; then
echo -n $t_bn: >>$si_log
cat $t >>$si_log 2>/dev/null
mv $t ${t%/*}/token.old >/dev/null 2>&1 ;vv=$?
if [ $vv != 0 ]; then
if [ "$err_token" ]; then
err_token="$err_token
$t_bn:rm_error:$rv"
else
err_token="$t_bn:rm_error:$rv"
fi
echo "token $t_bn, rm failed with rv=$rv" >>$si_log
fi
else
if [ "$err_token" ]; then
err_token="$err_token
$t_bn:not_found:127"
else
err_token="$t_bn:not_found:127"
fi
echo "token $t_bn, not found" >>$si_log
fi
done
timeout_v=0
if [ $n -lt $timeout_c ]; then
echo >&2
else
timeout_v=1
echo " timeout" >&2
echo "**** timeout: n=$n ****" >>$si_log
fi
if [ $timeout_v = 1 ]||[ "$wget_err" ]||[ "$err_token" ]; then
err_mesg="
$wget_err
$err_token"
err_mail "n=$n :timeout_v=$timeout_v" "$err_token"
fi
if [ $mkepgtab_v = 1 ]; then
/root/bin/mkepgtab.sh >>$si_log 2>&1 ;mv=$?
else
mv=00
fi
ex_date=`LANG=C date '+%Y-%m-%d %a %H:%M:%S'`
echo "${0##*/}-$version :$ex_date :$(($n*10))s :ended with mv=$mv" >>$si_log
set_log
exit 0
---
txt.cgi-0.1.5