Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Linux scripting script
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 4-May-11 20:48pm
tanujm492
Edited 4-May-11 21:10pm
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<pre>expect -re ".*#
puts $expect_out(buffer)</pre>
...
this is working for me now..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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>
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 495
1 Maciej Los 340
2 Richard MacCutchan 265
3 BillWoodruff 225
4 Mathew Soji 200
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 5 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100