I am using a shell script embedded an expect script to ssh to a remote server to config dmvpn(ipsec+mgre+nhrp), I also use temlate cli to be rendered as the configerations for each protocol. The structure of my files are as below:
dmvpn.sh: I used following codes to import template files
source ./dmvpn/ipsec_sec.cli
source ./dmvpn/ipsec_cnf.cli
source ./dmvpn/mgre.cli
source ./dmvpn/nhrp.cli
and following codes for rendering templates:
sec_cmd=`eval $sec`
cnf_cmd=`eval $cnf`
mgre_cmd=`eval $mgre`
nhrp_cmd=`eval $nh`
mgre.cli:
mgre='
if [ "$remote_ip" == "0.0.0.0" ]; then
echo "ip tunnel add $if_name mode gre local $local_ip key $key ttl $ttl";
else
echo "ip tunnel add $if_name mode gre local $local_ip remote $remote_ip key $key ttl $ttl";
fi;
echo "ip addr add $ip_mask dev $if_name";
echo "ip link set dev $if_name up";
echo "uci set network.$if_name=interface";
echo "uci set network.$if_name.ifname=$if_name";
echo "uci set network.$if_name.proto=static";
echo "uci set network.$if_name.ipaddr=$wip";
echo "uci set network.$if_name.netmask=$msk";
echo "uci commit network";
echo "/etc/init.d/network restart";
'
expect script in dmvpn.sh:
expect "#"
send "cat > /etc/ipsec.secrets << EOF
$sec_cmd
EOF\r"
expect "#"
send "sed -i \\"/conn ${ipsec_name}/,/ type/d\\" /etc/ipsec.conf \r"
expect "#"
send "cat >> /etc/ipsec.conf << EOF
$cnf_cmd
EOF\r"
expect "#"
send "ipsec reload\r"
expect "#"
send "$mgre_cmd\r"
expect "#"
send "$nhrp_cmd\r"
Each template files are designed in the same way, however, when executing $mgre_cmd, I've received following error:
root@OpenWrt:~# invalid command name "0"
while executing
"0"
invoked from within
"send "ip tunnel add gre1 mode gre local 10.25.110.1 key 123 ttl 255
ip addr add 2.2.12.1/24 dev gre1
ip link set dev gre1 up
uci set network.gre1=inte..."
$nhrp_cmd, $sec_cmd, $cnf_cmd didn't occur such error, I don't know why. nhrp.cli are as follows:
nhrp='
echo "vtysh \r
conf \r
int $if_name \r
ip nhrp network-id $network_id";
if [ "$nhs_wan_ip" == "0.0.0.0" ]; then
echo "ip nhrp nhs dynamic nbma $nhs_ip";
else
echo "ip nhrp nhs $nhs_wan_ip nbma $nhs_ip";
fi;
if $shortcut; then
echo "ip nhrp shortcut";
fi;
if $redirect; then
echo "ip nhrp redirect";
fi;
echo "tunnel source $tunnel_source \r
end \r
write \r
exit ";
'
The full expect script are as follows:
/usr/bin/expect<<-EOF
log_file dmvpn/${host}_add_dmvpn.log
spawn ssh -p $port $user@$host
expect {
-re "Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
}
"*password:" {
send "${loginpass}\r"
}
-re "Permission denied, please try again." {
exit
}
}
expect "#"
send "cat > /etc/ipsec.secrets << EOF
$sec_cmd
EOF\r"
expect "#"
send "sed -i \\"/conn ${ipsec_name}/,/ type/d\\" /etc/ipsec.conf \r"
expect "#"
send "cat >> /etc/ipsec.conf << EOF
$cnf_cmd
EOF\r"
expect "#"
send "ipsec reload\r"
expect "#"
send "$mgre_cmd\r"
expect "#"
send "$nhrp_cmd\r"
expect "#"
send "exit\r"
expect eof
EOF
The result of echo $mgre_cmd
:
ip tunnel add gre1 mode gre local 10.25.110.1 key 123 ttl 255 ip addr add 2.2.12.1/24 dev gre1 ip link set dev gre1 up uci set network.gre1=interface uci set network.gre1.ifname=gre1 uci set network.gre1.proto=static uci set network.gre1.ipaddr=2.2.12.1 uci set network.gre1.netmask=255.255.255.0 uci commit network /etc/init.d/network restart
When I use /usr/bin/expect -d <<-EOF
I got things below:(only execute $mgre_cmd)
spawn ssh -p 22 root@10.25.110.1
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {15176}
Gate keeper glob pattern for 'Are you sure you want to continue connecting (yes/no)?' is 'Are you sure you want to continue connecting *'. Activating booster.
Gate keeper glob pattern for 'Permission denied, please try again.' is 'Permission denied, please try again?'. Activating booster.
expect: does "" (spawn_id exp7) match regular expression "Are you sure you want to continue connecting (yes/no)?"? Gate "Are you sure you want to continue connecting *"? gate=no
"*password:"? no
"Permission denied, please try again."? Gate "Permission denied, please try again?"? gate=no
root@10.25.110.1's password:
expect: does "root@10.25.110.1's password: " (spawn_id exp7) match regular expression "Are you sure you want to continue connecting (yes/no)?"? Gate "Are you sure you want to continue connecting *"? gate=no
"*password:"? yes
expect: set expect_out(0,string) "root@10.25.110.1's password:"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) "root@10.25.110.1's password:"
send: sending "zzs\r" to { exp7 }
expect: does " " (spawn_id exp7) match glob pattern "#"? no
expect: does " \r\n" (spawn_id exp7) match glob pattern "#"? no
_______ ________ __
| |.-----.-----.-----.| | | |.----.| |_
| - || _ | -__| || | | || _|| _|
|_______|| __|_____|__|__||________||__| |____|
|__| W I R E L E S S F R E E D O M
-----------------------------------------------------
uCPE 1.0.0, r11063-85e04e9f46
-----------------------------------------------------
expect: does " \r\n _______ ________ __\r\n | |.-----.-----.-----.| | | |.----.| |_\r\n | - || _ | -__| || | | || _|| _|\r\n |_______|| __|_____|__|__||________||__| |____|\r\n |__| W I R E L E S S F R E E D O M\r\n -----------------------------------------------------\r\n uCPE 1.0.0, r11063-85e04e9f46\r\n -----------------------------------------------------\r\n" (spawn_id exp7) match glob pattern "#"? no
]0;root@OpenWrt: ~root@OpenWrt:~#
expect: does " \r\n _______ ________ __\r\n | |.-----.-----.-----.| | | |.----.| |_\r\n | - || _ | -__| || | | || _|| _|\r\n |_______|| __|_____|__|__||________||__| |____|\r\n |__| W I R E L E S S F R E E D O M\r\n -----------------------------------------------------\r\n uCPE 1.0.0, r11063-85e04e9f46\r\n -----------------------------------------------------\r\n\u001b]0;root@OpenWrt: ~\u0007root@OpenWrt:~# " (spawn_id exp7) match glob pattern "#"? yes
expect: set expect_out(0,string) "#"
expect: set expect_out(spawn_id) "exp7"
expect: set expect_out(buffer) " \r\n _______ ________ __\r\n | |.-----.-----.-----.| | | |.----.| |_\r\n | - || _ | -__| || | | || _|| _|\r\n |_______|| __|_____|__|__||________||__| |____|\r\n |__| W I R E L E S S F R E E D O M\r\n -----------------------------------------------------\r\n uCPE 1.0.0, r11063-85e04e9f46\r\n -----------------------------------------------------\r\n\u001b]0;root@OpenWrt: ~\u0007root@OpenWrt:~#"
invalid command name "0"
while executing
"0"
invoked from within
"send "ip tunnel add gre1 mode gre local 10.25.110.1 key 123 ttl 255\r
ip addr add 2.2.12.1/24 dev gre1 \r ip link set dev gre1 up \r uci set network.g..."
Finally, I made it! Though I still don't know what's difference between mgre.cli and other clis.
I changed this command send "$mgre_cmd \r"
into send -- {$mgre_cmd}
and send -- \r
I noticed that while executing expect commands or tcl commands in a shell script we sometimes need to use escape characters to fix the commands. We can use send -- {}
to translate the meaning of the whole command.