expect定期远程登录Juniper防火墙实现定期重新拨号 发表于 2015-05-21 | 分类于 小技巧 | | 阅读次数 | 字数统计 680 | 阅读时长 3 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133#!/bin/bash#author: GaoMing#date: 2015-05-20#qq: 530035210#blog: http://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=adminHost=192.168.1.253Password="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 $logdirecho "[ $(datef) ] $1" >> $logfiif [[ $is_font -eq 1 ]];thenecho -e "[ $(datef) ] $1"fi}run(){if [[ $1 == "" ]];thenprint_log "cmd命令不能为空"exitfi 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=$1email_content=$2email_subject=$3if [[ $email_title == "" ]];thenemail_title="这是一封测试邮件."fiif [[ $email_content == "" ]];thenemail_content="这是测试邮件的内容."fiif [[ $email_subject == "" ]];thenemail_subject="这是测试邮件的主题."fiecho -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 == "" ]];thenprint_log "eth0/1: 拨号已经断开!"sendmail "" "eth0/1:重新拨号断开." "ppoe-eth1-重新拨号已经断开"fiif [[ $interface2_2 == "" ]];thenprint_log "eth0/2: 拨号已经断开!"sendmail "" "eth0/2:重新拨号断开." "ppoe-eth2-重新拨号已经断开"fiif [[ $interface2_3 == "" ]];thenprint_log "eth0/3: 拨号已经断开!"sendmail "" "eth0/3:重新拨号断开." "ppoe-eth3-重新拨号已经断开"fiif [[ $interface2_1 != "$interface_1" ]];thenprint_log "eth0/1:重新拨号成功!"elseprint_log "eth0/1:重新拨号失败,拨号前后外网IP没有发生变化!"sendmail "" "eth0/1:重新拨号失败,IP没有发生变化." "ppoe-eth1-重新拨号失败"fi if [[ $interface2_2 != "$interface_2" ]];thenprint_log "eth0/2:重新拨号成功!"elseprint_log "eth0/2:重新拨号失败,拨号前后外网IP没有发生变化!"sendmail "" "eth0/2:重新拨号失败,IP没有发生变化." "ppoe-eth2-重新拨号失败"fiif [[ $interface2_3 != "$interface_3" ]];thenprint_log "eth0/3:重新拨号成功!"elseprint_log "eth0/3:重新拨号失败,拨号前后外网IP没有发生变化!"sendmail "" "eth0/3:重新拨号失败,IP没有发生变化." "ppoe-eth3-重新拨号失败"fi 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏