forked from lmangani/acme-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
161 lines (133 loc) · 2.14 KB
/
functions.sh
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
##
## Site specific config here
##
# The user name and password
user=acme
password=packet
# The HA config address for the N-N
host=10.100.0.6
# The N-N community
community=j3110
# The config address
admin_host=10.17.0.163
# This is where to store the config archives
config_root=/export/config/acme
# Set this to the root of where the scripts live
root=/export/provisioning/acme
# The vland of the core network
core_vlan=405
# The default SIP port on the access side
access_port=5060
# The default SIP port on the core side
core_port=5060
##
## Don't change below here
##
set -u
trim () { echo $1; }
cfg_prompt="(configure)#"
tmp=$root/tmp
tmpfile=$tmp/$$.exp
logfile=$tmp/$$.log
base=$(basename $0)
on_error() {
echo "ERROR:"
cat $tmp/*.log
rm -f $tmp/*.log
}
trap on_error ERR
cleanup_tmp () {
rm -f $tmpfile
}
create_tmp() {
mkdir -p $tmp
> $tmpfile
> $logfile
trap cleanup_tmp EXIT
}
execute() {
cat <<EOF > $tmpfile
set timeout 5
proc abort {} {
puts "Timeout or EOF\n"
exit 1
}
spawn telnet $host
expect {
Password: { send "$user\r" }
default abort
}
expect {
> { send "en\r"; exp_continue }
Password: { send "$password\r" }
default abort
}
$*
expect {
"#" { send "exit\r"; exp_continue }
-ex {Save Changes [y/n]?:} { send "y\n" ;exp_continue }
">" { send "exit\r" }
default abort
eof
}
EOF
expect -f $tmpfile
#> $logfile
rm $logfile
}
cfg_cmd() {
prompt=$1
cmd=$2
cat <<EOF
expect {
"$prompt" { send "$cmd\r" }
default abort
}
EOF
}
configure() {
cat <<EOF
expect {
"#" { send "configure term\r"}
default abort
}
$*
expect {
"$cfg_prompt" { send "exit\r"}
default abort
}
EOF
}
activate() {
cat <<EOF > $tmpfile
set timeout 10
proc abort {} {
puts "Timeout or EOF\n"
exit 1
}
sleep 1
spawn telnet $host
expect {
Password: { send "$user\r" }
default abort
}
expect {
> { send "en\r"; exp_continue }
Password: { send "$password\r" }
default abort
}
expect {
"#" { send "activate-config\r"}
default abort
}
expect {
-ex { continue (y/n) } { send "y\r"; exp_continue}
"#" { send "exit\r"; exp_continue }
">" { send "exit\r" }
default abort
eof
}
EOF
expect -f $tmpfile >> $logfile
rm $logfile
}