Click here to Skip to main content
15,886,792 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi all,
i am facing some problem while using send in expect (tcl).
problem:
1. in linux machine i am running some process refelct: for getting the process id of all the reflect processes i m using
ps -aef | grep reflect | grep -v grep | awk '{print $2}'
which will return something like

19768
19546
10465

2. same thing i want to do with the help pf expect , here is my code
proc monitor_server {ip_server} {
	source "/cygdrive/e/ncp/pat/pat_config.exp"
	set cmd1 {ps -aef | grep reflect | grep -v grep | awk '{print $2}'}
	if {[catch { spawn ssh $ip_server -l root } res]} {
			puts $res
			return 0
		} else {
			expect -re "(yes/no)"
			send "yes\r"
			sleep 1
			expect -re "root@$ip_server's password: " 
			send "$passwd_testbed\r"
			expect -re ".*# " {
				send " $cmd1 \r"
				expect -re "/\d\n" 
				puts $expect_out(0,string)
			}		}
}

this code is working up to executing the command, but when the command is getting executed , i want to get the out put in variable $expect_out(0,string).
may be i need some kind on pattern matching for that or is there any other way to get the result in some variable while i send the $cmd (i mean result of the command).
as i am new to tcl and expect.
thanks in advance
tanuj
Posted
Updated 4-May-11 20:10pm
v4

<pre>expect -re ".*#
puts $expect_out(buffer)</pre>
...
this is working for me now..
 
Share this answer
 
so finally i got the solution:
<code>
proc monitor_server {ip_server} {
source "/cygdrive/e/ncp/pat/pat_config.exp"
set cmd1 {ps -aef | grep reflect | grep -v grep | awk '{print $2}'}
if {[catch { spawn ssh $ip_server -l root } res]} {
puts $res
return 0
} else {
expect -re "(yes/no)"
send "yes\r"
sleep 1
expect -re "root@$ip_server's password: "
send "$passwd_testbed\r"
expect -re ".*# " {
send " $cmd1 \r"
expect -re ".*#"
regexp {(\n)(\d.*)} "$expect_out(buffer)" matched
set values [split $matched "\r"]
expect ".*#"

} }
}</code>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900