You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Expect命令批量配置Zyxel AP时MAC地址变量传递异常问题

解决Expect传递含冒号MAC地址的问题

问题根源分析

你的脚本存在两个关键问题导致MAC地址传递异常:

  1. 使用中文全角引号:脚本中send „super_secure “里的引号是中文格式,Expect无法正确识别字符串边界,会导致命令执行逻辑混乱,进而影响后续变量传递。
  2. 换行符格式错误:脚本里的 是不可见的异常特殊字符,应该使用Expect标准的\r表示回车换行。

修正后的脚本

#!/usr/bin/expect

# Usage block-mac.expect <mac-adress> <hostname-accesspoint>

set macadress [lindex $argv 0]
set accesspoint [lindex $argv 1]

puts "AP Hostname: $accesspoint"
puts "MAC Address: $macadress"

spawn ssh admin@$accesspoint.mydomain.com

expect "*assword:" { send "super_secure\r" }
expect "Router" { send "enable\r" }
expect "Router#" { send "configure terminal\r" }
expect "Router(config)" { send "wlan-macfilter-profile mac-filter\r" }
expect "Router(config-wlan-macfilter mac-filter)#" { send "filter-action deny\r" }
expect "Router(config-wlan-macfilter mac-filter)#" { send -- "$macadress\r" }

额外优化建议

  • 调用脚本时,为兼容所有场景,可使用单引号包裹MAC参数(虽然冒号不属于Shell特殊字符,但能避免意外解析):
    expect block-mac.expect 'aa:bb:cc:dd:ee:ff' accpoint-01
    
  • 在send命令中添加--前缀(如send -- "$macadress\r"),可避免MAC地址以-开头时被误判为Expect命令选项。
  • 可增加超时处理,防止脚本卡在某个expect步骤:
    set timeout 10
    expect {
        "*assword:" { send "super_secure\r" }
        timeout { puts "Timeout waiting for password prompt"; exit 1 }
    }
    

内容的提问来源于stack exchange,提问作者vtmb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 03:57:45