Maestro_T 2020-09-08 11:50 采纳率: 100%
浏览 1252
已采纳

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条回答 默认 最新

  • dabocaiqq 2020-09-08 12:52
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#单片机#的问题:K210 例程里面提示我iomem.h: No such file or directory
  • ¥15 LSPI算法的大问题
  • ¥15 java导出word 动态填充数据
  • ¥15 python SSH连接设备使用STD读配置卡死的问题
  • ¥20 扑克的算牌公式及软件制作
  • ¥20 如何通过云图中RPG去计算云图上不同位置的值?
  • ¥15 请问不小心下载到了钓鱼软件怎么办?
  • ¥15 求国博抢票 求国博抢票 有的私
  • ¥50 swiftui @query 报错
  • ¥50 怎么解决刷卡或扫码后,点击软件输入框,win10屏幕键盘不会自动弹出的问题