mercredi 25 mars 2015

grep command with pipes, how to implement in script?


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" where 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.



#!/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


Aucun commentaire:

Enregistrer un commentaire