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

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
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格