前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >expect定期远程登录Juniper防火墙实现定期重新拨号

expect定期远程登录Juniper防火墙实现定期重新拨号

作者头像
明哥的运维笔记
发布2019-01-30 10:06:29
5000
发布2019-01-30 10:06:29
举报
文章被收录于专栏:运维笔记运维笔记
代码语言:javascript
复制
#!/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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-05-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档