#################################
#Check current route of mail svr
#Return $rt=[fib|dsl]
#################################
function checkrt( )
{
rt=fib
echo "`date` :[checkrt( ) ]: Running checkrt( )" | tee -a $log
netstat -rn | grep 2xx.xx.xx.12 >/dev/null
a=`echo $?`
if [ $a -eq "0" ]; then
rt=dsl
else
rt=fib
fi
echo "`date` :[checkrt( ) ]: Return \$rt is $rt" | tee -a $log
}
##########################
#Check mail server status
#Return $stat[err|ok]
##########################
function checkstat( )
{
num="0"
stat="err"
echo "`date` :[checkstat( )]: Running checkstat( )" | tee -a $log
$home/getstatus >/tmp/mailsvrstatus
num=`tail -n1 /tmp/mailsvrstatus | cut -c1-3`
echo $num
if [ $num != "220" ]; then
stat=err
else
stat=ok
fi
echo "`date` :[checkstat( )]: Returen \$stat is $stat" | tee -a $log
}
#################################
# Caculate seconds from last err
# Return $secs
#################################
function calctime( )
{
secs=0
echo "`date` :[calctime( ) ]: Running calctime( )" | tee -a $log
begin=`cat /tmp/errtime`
end=`date +%s`
((secs=$end-$begin))
export secs
echo "`date` :[calctime( ) ]: Retuen \$secs $secs" | tee -a $log
}
###########################
#Get DSL ip
###########################
function getdslip( )
{
echo "`date` :[getdslip( ) ]: Running getdslip( )" | tee -a $log
dslip=`$home/getdslstat | grep "Remote IP" | awk '{ print $3 }'`
echo "`date` :[getdslip( ) ]: Return \$dslip dslip" | tee -a $log
}
##########################
# Main
# Last modi:2006/7/28
##########################
stat="err"
while [ $stat = "err" ]
do
echo '------------------------------------------------------------------------'| tee -a $log
checkstat
if [ $stat = "err" ]; then
if [ -e /tmp/errtime ]; then
echo "`date`: [main( ) ]: File Errtime existing" | tee -a $log
calctime
else
echo `date +%s` >/tmp/errtime
secs=0
echo "`date` :[main( ) ]: File Errtime Created" | tee -a $log
fi
else
if [ -e /tmp/errtime ]; then
rm /tmp/errtime
echo "`date`: [main( ) ]: File Errtime Removed" | tee -a $log
fi
checkrt
if [ $rt = "dsl" ]; then
getdslip
fi
sleep 600
exec $home/chip4mail
fi
checkrt
if [ $rt = "dsl" ]; then
if [ $secs -gt "7200" ]; then
rm /tmp/errtime
$home/tofib
echo " `date` :[main( ) ]: Changed to fiber" | tee -a $log
else
echo "`date` :[main( ) ]: Redial DSL" | tee -a $log
$home/redial >/dev/null
sleep 30
getdslip
fi
else
$home/todsl
echo "`date` :[main( ) ]: Changed to DSL" | tee -a $log
getdslip
fi
done
::::::::::::::
redial
::::::::::::::
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Fri Jul 28 14:32:51 2006
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script. It
# necessarily has to guess about certain things. Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts. If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character. This
# pacifies every program I know of. The -c flag makes the script do
# this in the first place. The -C flag allows you to define a
# character to toggle this mode off and on.
set force_conservative 0 ;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
#
# 2) differing output - Some programs produce different output each time
# they run. The "date" command is an obvious example. Another is
# ftp, if it produces throughput statistics at the end of a file
# transfer. If this causes a problem, delete these patterns or replace
# them with wildcards. An alternative is to use the -p flag (for
# "prompt" which makes Expect only look for the last line of output
# (i.e., the prompt). The -P flag allows you to define a character to
# toggle this mode off and on.
#
# Read the man page for more info.
#
# -Don
set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "\]0;root/usr/local/chip4mail \[root@host chip4mail\]# "
send -- "telnet 192.168.x.x\r"
expect -exact "telnet 192.168.xx.xx\r
Trying 192.168.x.x...\r\r
Connected to 192.168.xx.xx.\r\r
Escape character is '^\]'.\r\r
\r
\r
Login: "
send -- "username\r"
expect -exact "username\r\r
Password: "
send -- "x"
expect -exact "*"
send -- "x"
expect -exact "*"
send -- "x"
expect -exact "*"
send -- "x"
expect -exact "*"
send -- "x"
expect -exact "*"
send -- "\r"
expect -exact "\r\r
\r\r
Login successful\r\r
\r\r
--> "
send -- "pppoe set transport 1 disable\r"
expect -exact "pppoe set transport 1 disable\r\r
\r\r
--> "
send -- "pppoe set transport 1 enable\r"
expect -exact "pppoe set transport 1 enable\r\r
\r\r
--> "
send -- "user logout\r"
expect -exact "user logout\r\r
\r
Logging out.\r
\r
Connection closed by foreign host.\r\r
\]0;root:/usr/local/chip4mail \[root@ chip4mail\]# "
send -- "exit\r"
expect eof