Maestro_T
Maestro_T
采纳率100%
2020-09-08 11:50

Expect脚本错误:invalid command name "0"

已采纳

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

dmvpn.sh
ipsec_sec.cli   用于配置ipsec.secrets
ipsec_cnf.cli   用于配置ipsec.conf
mgre.cli        用于配置mgre
nhrp.cli        用于配置nhrp

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

source ./dmvpn/ipsec_sec.cli
source ./dmvpn/ipsec_cnf.cli
source ./dmvpn/mgre.cli
source ./dmvpn/nhrp.cli

渲染模板的代码:

sec_cmd=`eval $sec`
cnf_cmd=`eval $cnf`
mgre_cmd=`eval $mgre`
nhrp_cmd=`eval $nhrp`

expect远程配置命令如下:

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"

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";
echo "uci add_list firewall.zone[0].network=$if_name";
echo "uci commit firewall";
echo "/etc/init.d/firewall restart";
'

nhrp.cli内容如下:

nhrp='
echo "uci set network.$if_name=interface";
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 ";
'

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

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..."

执行其他的$sec_cmd,$cnf_cmd, $nhrp_cmd均没有报错,求大神指教!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

为你推荐