#!/bin/bash
#author: GaoMing
#date: 2015-05-20
#qq: 530035210
#blog: https://my.oschina.net/pwd/blog
#pppoe 定期重新拨号
logdir=/data/log/shell #日志路径
log=$logdir/log.log #日志文件
is_font=1 #终端是否打印日志: 1打印 0不打印
is_log=1 #是否记录日志: 1记录 0不记录
User=admin
Host=192.168.1.253
Password="cccccc!@#xxxxxxxx748."
#Cmd="get pppoe name ADSL-callcenter"
Cmd="get interface"
from_name="运维组"
from="530035210@qq.com"
to="huanggm@baoxian.com"
datef(){
date "+%Y-%m-%d %H:%M:%S"
}
print_log(){
if [[ $is_log -eq 1 ]];then
[[ -d $logdir ]] || mkdir -p $logdir
echo "[ $(datef) ] $1" >> $log
fi
if [[ $is_font -eq 1 ]];then
echo -e "[ $(datef) ] $1"
fi
}
run()
{
if [[ $1 == "" ]];then
print_log "cmd命令不能为空"
exit
fi
expect -c "
spawn /usr/bin/ssh -o StrictHostKeyChecking=no $User@$Host
set timeout -1
expect \"\*password\*:\"
send \"$Password\r\"
expect \"\*\>\"
send \"$1 \r\"
expect \"\*\>\"
send \"exit\r\"
expect eof
"
}
sendmail(){
email_title=$1
email_content=$2
email_subject=$3
if [[ $email_title == "" ]];then
email_title="这是一封测试邮件."
fi
if [[ $email_content == "" ]];then
email_content="这是测试邮件的内容."
fi
if [[ $email_subject == "" ]];then
email_subject="这是测试邮件的主题."
fi
echo -e "To: \"${email_title}\" <${to}> \nFrom: \"${from_name}\" <${from}>\nSubject: ${email_subject}\n\n`echo ${email_content}`" | /usr/sbin/sendmail -t
}
#获取拨号前接口的外网地址
run_res1=$(run "get interface")
interface_1=$(echo "$run_res1" |grep "eth0\/1" |awk '{print $2}' |sed "s/\/32//g")
interface_2=$(echo "$run_res1" |grep "eth0\/2" |awk '{print $2}'|sed "s/\/32//g")
interface_3=$(echo "$run_res1" |grep "eth0\/3" |awk '{print $2}'|sed "s/\/32//g")
print_log "重启前,各接口的外网地址如下: eth0/1:$interface_1 eth0/2:$interface_2 eth0/3:$interface_3"
#开始拨号
run_int1=$(run "exec pppoe name ADSL-bw2 disconnect")
run_int2=$(run "exec pppoe name BW-GuangXian-2 disconnect")
run_int3=$(run "exec pppoe name ADSL-callcenter disconnect")
#获取拨号后接口的外网地址
run2_res1=$(run "get interface")
interface2_1=$(echo "$run2_res1" |grep "eth0\/1" |awk '{print $2}' |sed "s/\/32//g")
interface2_2=$(echo "$run2_res1" |grep "eth0\/2" |awk '{print $2}'|sed "s/\/32//g")
interface2_3=$(echo "$run2_res1" |grep "eth0\/3" |awk '{print $2}'|sed "s/\/32//g")
print_log "重启后,各接口的外网地址如下: eth0/1:$interface2_1 eth0/2:$interface2_2 eth0/3:$interface2_3"
if [[ $interface2_1 == "" ]];then
print_log "eth0/1: 拨号已经断开!"
sendmail "" "eth0/1:重新拨号断开." "ppoe-eth1-重新拨号已经断开"
fi
if [[ $interface2_2 == "" ]];then
print_log "eth0/2: 拨号已经断开!"
sendmail "" "eth0/2:重新拨号断开." "ppoe-eth2-重新拨号已经断开"
fi
if [[ $interface2_3 == "" ]];then
print_log "eth0/3: 拨号已经断开!"
sendmail "" "eth0/3:重新拨号断开." "ppoe-eth3-重新拨号已经断开"
fi
if [[ $interface2_1 != "$interface_1" ]];then
print_log "eth0/1:重新拨号成功!"
else
print_log "eth0/1:重新拨号失败,拨号前后外网IP没有发生变化!"
sendmail "" "eth0/1:重新拨号失败,IP没有发生变化." "ppoe-eth1-重新拨号失败"
fi
if [[ $interface2_2 != "$interface_2" ]];then
print_log "eth0/2:重新拨号成功!"
else
print_log "eth0/2:重新拨号失败,拨号前后外网IP没有发生变化!"
sendmail "" "eth0/2:重新拨号失败,IP没有发生变化." "ppoe-eth2-重新拨号失败"
fi
if [[ $interface2_3 != "$interface_3" ]];then
print_log "eth0/3:重新拨号成功!"
else
print_log "eth0/3:重新拨号失败,拨号前后外网IP没有发生变化!"
sendmail "" "eth0/3:重新拨号失败,IP没有发生变化." "ppoe-eth3-重新拨号失败"
fi