首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过使用Expect来配置mgre的奇怪错误:无效命令名"0“

通过使用Expect来配置mgre的奇怪错误:无效命令名"0“
EN

Stack Overflow用户
提问于 2020-09-08 03:23:26
回答 1查看 90关注 0票数 1

我使用一个shell脚本将expect脚本嵌入到远程服务器上,以配置dmvpn(ipsec+mgre+nhrp),我还使用temlate作为每个协议的配置。我的文件结构如下:

  • dmvpn.sh
  • ipsec_sec.cli ( ipsec.secrets)
  • ipsec_cnf.cli模板文件)( ipsec.conf)
  • mgre.cli模板文件(mgre模板文件)
  • nrhp.cli(nhrp模板文件)

dmvpn.sh:我使用以下代码导入模板文件

代码语言:javascript
复制
source ./dmvpn/ipsec_sec.cli
source ./dmvpn/ipsec_cnf.cli
source ./dmvpn/mgre.cli
source ./dmvpn/nhrp.cli

以及用于绘制模板的下列代码:

代码语言:javascript
复制
sec_cmd=`eval $sec`
cnf_cmd=`eval $cnf`
mgre_cmd=`eval $mgre`
nhrp_cmd=`eval $nh`

mgre.cli:

代码语言:javascript
复制
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";
'

在dmvpn.sh中期望脚本:

代码语言:javascript
复制
   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_cmd时,我收到了以下错误:

代码语言:javascript
复制
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没有发生这样的错误,我不知道为什么。nhrp.cli如下:

代码语言:javascript
复制
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 ";
'

完整的expect脚本如下:

代码语言:javascript
复制
/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

echo $mgre_cmd的结果

代码语言:javascript
复制
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

当我使用/usr/bin/expect -d <<-EOF时,我得到了以下内容:(只执行$mgre_cmd)

代码语言:javascript
复制
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..."
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-08 08:27:54

终于,我做到了!尽管我仍然不知道mgre.cli和其他clis之间有什么区别。我将这个命令send "$mgre_cmd \r"改为send -- {$mgre_cmd}send -- \r,我注意到在执行shell脚本中的expect命令或tcl命令时,我们有时需要使用转义字符来修复这些命令。我们可以使用send -- {}来翻译整个命令的含义。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63786687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档