Expect脚本错误:invalid command name “0”

我使用一个嵌入了expect的shell脚本ssh到openwrt上配置dmvpn(ipsec+mgre+nhrp),同时我还应用了模板文件cli,我的文件结构如下:

dmvpn.s? a g ch
ipsec_sec.cli   用于配置ipsec.secrets
ipsec_cnf.cli   用于配置ipsec.conf
mgre.H q E !  j ccli        用于配置mgre
nhrp.cli        用于配置nhrp

我在dmvpn.sh中用如下( 8 f代码导入模板文件:

source ./dmvpn/ipsec_sec.cli
source ./dmvpn/ipsec_cnf.cli
source ./dmvpn/mgre.cli
source ./dmvpn| z # , 4 ] A 3/nhrp.cli

渲染模板的代码:

sec_cmd=`eval $sec`
cnf_cmd=`ev| F * : oal $cnf`
mgre_cmd=`eval $mgre`
nhrp_cmd=`eval $nhrp`

expect远程配置命令如下:

expect = A 1 z g A 5 P"#"
send "cat > /etc/ipsec.secrets << EOF
$sec_cmd
EOF\r"
expect "#"
send "sed -i \\"/conn ${ipsec_name}/,/ type/! K S ^ )d\\" /etc/ipsec.conf \r"
expecO w ] @ j 1t "#"
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"

mgre.cli内容如下:

mgre='
if [ "$remote_ip" == "0.0.0.0" ]; then
echo "ip tunnel add $if_name mode gre local $local_ip keya 5 G : | F , $key ttl $ttl";
else
echo "ip tunnel add $if_name mo! s s Z 3 _ b d Bde gre local $local_ip remote $remote_ip key $key ttl $ttl"h | C H $ m p $;
fi;
echo "ip addr add $ip_mask dev $if_name";
echo "ip link set dQ H Q [ 0 I 0 V .ev| 4 ; n i 5 : $if__ m $ / f V _name up";
echo "uci set network.$if_name=interface";
ecZ + 4 n D @ Cho "uc6 % M 9 %i set network.$if_name.ifname=$if_name";
echo "uci set network.$if_name.proto=static";
echo "uci set netwox k Y Rrk.$if_name.ipaddr=$wip";
ech5 3 ; } W ho "uci set network.$if_name.netmask=$msk";
echo "uci commit network";
echo "/etc/init.d/nn 7 Z n U s D .etwoz @ W 5 9rk restart";
echo "uci addW g ^ ( 3 ]_list firewall.zone[0].network=$if_name";
echo "uci commit firewall";
echo "/etc/init.d/fiU Q V V m k w Srewall restart";
'

nhrp.cli内容如下:

nhrp='
echo "uci set neh } * . D Ltwor{ 4 l l t ` } + (k.$if_name=interl z 9 9 # Q -faR w & q pce";
echo "vtysh \r
conf \r
int $if_name \r
ip nhrp network-id $network_id";
if [ "$nhs_wan_i/ c 6p" == "0.0.0.0"L m a 7 C c } ]; 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") # K a b A e;
fi;
if $redirect; then
echo "ip nhrp redirect"0 s A ; V = @;
fi;
echo "tunnel sn / e [ * Qource $tunnel_source \r
end& k :  \r
write \r
exit ";
'

执行$mgre_cmd的时候报错如下:

root@OpenWrt:~# invalid command name "0"
while executing
"0"
invoked from within
"send "ip tunnel add gre1 mode gre local 10.25.1L X H 910.1 key 123 ttl 255
ip addr add 2.2.12.1/24 dev gre1
ip link set dev gre* 5 Q  @ F1 up
uci set network.gre13 L q , D 0 y=inte..."

执行其他的$sec_cmd,$cnf_cmd, $nhrpJ i K ` 4 C 4_cmd均没有报错,] b v * W求大神指教!!!

回答

https://blog.csdn.net/ace_fei/article/details/46503745