sending GREP command fails

1

I'm new to expect scripting and trying to get below script to work. The script logs in to a server and reads a config file ($val in the script) for the values of "cps" and then presents a total value, but got stuck when sending a long "grep" command which I split into 2 parts:

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}

The execution stops at:

exp_send "$cmd1 $val $cmd2\r"  

here only the 1st part ($cmd1 $val) is executed, the 2nd part ($cmd2) sends as a separate command and fails at "|" with message: bash: syntax error near unexpected token |'

The grep` command works OK if executed directly on Linux. I suppose there is an error (syntax ?) in the exp_send line but haven't figured out how to do it in a proper way, there is for sure a better way to do it. This is the actual script:

#!/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 user@192.24.135.166  
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   

Dalman

Posted 2015-04-15T07:04:15.190

Reputation: 11

Does the contents of $val end with a \r? Are you reading from a file that had DOS line endings? – glenn jackman – 2015-04-15T10:18:51.740

Hi Glenn, thanks for taking the time to reply, the value of $val is the name of a text file e.g: ts1455_TrafficMix_ims.cfg the content of that file is in plain text. – Dalman – 2015-04-15T11:27:16.993

Answers

0

I think I'm right:

expect -re "(Titan.*)\r"  
set val $expect_out(0,string)  

expect_out(0,string) will contain the whole match, not just the bit in parentheses.

Try:

set val $expect_out(1,string)

to only select what matched the pattern in (the first set of) parentheses.

glenn jackman

Posted 2015-04-15T07:04:15.190

Reputation: 18 546