使用Expect命令批量配置Zyxel AP时MAC地址变量传递异常问题
解决Expect传递含冒号MAC地址的问题
问题根源分析
你的脚本存在两个关键问题导致MAC地址传递异常:
- 使用中文全角引号:脚本中
send „super_secure “里的引号是中文格式,Expect无法正确识别字符串边界,会导致命令执行逻辑混乱,进而影响后续变量传递。 - 换行符格式错误:脚本里的
是不可见的异常特殊字符,应该使用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
相关产品推荐
相关产品推荐

