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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/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=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
坚持原创技术分享,您的支持将鼓励我继续创作!