我是新手,想尝试让下面的脚本工作。脚本登录到服务器并读取配置文件 ($val 中的脚本) 以获取“cps”的值,然后显示总值,但在发送长“grep”命令时卡住了,我将其分成两部分:
set cmd1 "grep -A200 \"_ims\""
set cmd2 { | grep -B3 "calledUserDescriptor" | grep "cps" | grep -v "//cps" | awk '{ SUM += $3} END { print "total sip cps = "SUM}' >> cps.txt}
执行停止于:
exp_send "$cmd1 $val $cmd2\r"
这里只执行了第一部分 ($cmd1 $val),第二部分($cmd2
)作为单独的命令发送并在“|”处失败并显示消息:bash: syntax error near unexpected token |'
如果直接在 Linux 上执行,grep` 命令可以正常工作。我猜想 exp_send 行中有一个错误(语法?),但还没有弄清楚如何以正确的方式执行它,肯定有更好的方法。这是实际的脚本:
#!/usr/bin/expect -f
set lnk1 "Traffic_ims.cfg"
set cmd1 "grep -A200 \"_ims\""
set cmd2 { | grep -B3 "calledUserDescriptor" | grep "cps" | grep -v "//cps" | awk '{ SUM += $3} END { print "total sip cps = "SUM}' >> cps.txt}
set file1 "/tmp/cps.txt"
set cmd3 "cd /home/traffic/"
set cmd4 "readlink"
set passwd "xxxxx"
log_user 1
spawn rm -rf $file1
spawn ssh [email protected]
expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\n"
exp_continue
}
"*?assword:*" {
send $passwd
send "\n"
}
}
expect "*\$ "
exp_send "$cmd3\r"
expect "$cmd3\r"
expect -re $
exp_send "$cmd4 $lnk1\r"
expect "$cmd4 $lnk1\r"
expect -re "(Titan.*)\r"
set val $expect_out(0,string)
exp_send "$cmd1 $val $cmd2\r"
expect -re "(.*)\r"
set output [open "/tmp/cps.txt" "a+"]
set outcome $expect_out(buffer)
send "\r"
puts $output $outcome
close $output
exp_send "exit \r"
exit 0
答案1
我认为我是对的:
expect -re "(Titan.*)\r"
set val $expect_out(0,string)
expect_out(0,string)
将包含整个匹配项,而不仅仅是括号中的位。
尝试:
set val $expect_out(1,string)
仅选择与(第一组)括号中的模式匹配的内容。