Expect: how to print every output of a spawned process to the user - tcl

I'm trying to write an expect script, part of the commands will be executed as a different user. So i need to spawn an kuu process, then send commands to it after user provided password. But how do I collect the output of those commands and print it to the user who is running the expect scripts?
Thanks

This can be done by using something like the following:
proc outputUntilPrompt {} {
global expect_out
set prompt "ACT:*>*"
set output ""
while 1 {
expect {
-re "(\[^\r]*\)\r\n" {
append output $expect_out(buffer)
}
$prompt {
append output $expect_out(buffer)
break
}
}
}
return $output
}
send_user "$output"

Related

How to get responses in expect (tcl)

I am trying to query bluetoothctl using expect (tcl), but I cannot seem to get the bluetoothctl responses saved to a variable for processing with tcl.
For example:
spawn bluetoothctl
exp_send "scan on\n"
expect {
-re {*NEW*} {
set new $expect_out(0,string)
puts "scan - found $new"
exp_continue
}
timeout {
exp_send "scan off\n"
exp_send "quit\n"
close
wait
puts "EXPECT timed out"
}
}
The result of the above is along the lines of:
[bluetooth]# scan on
Discovery started
[CHG] Controller 10:08:B1:57:35:62 Discovering: yes
[NEW] Device EB:06:EF:34:04:B7 MPOW-059
[bluetooth]#
EXPECT timed out
So nothing is output until expect is closed. I have been trying this all day with different combinations but - I am stuck. Any help would be appreciated. Thanks
Edit: changed the regex to (.NEW.) and that works. So now I get:
[bluetooth]# scan on
Discovery started
[CHG] Controller 10:08:B1:57:35:62 Discovering: yes
[NEW] Device EB:06:EF:34:04:B7 MPOW-059
[bluetooth]# scan - found scan on
Agent registered
[bluetooth]# scan on
Discovery started
[CHG] Controller 10:08:B1:57:35:62 Discovering: yes
[NEW
which is everything except the bit that I wanted to retrieve viz:
[NEW] Device EB:06:EF:34:04:B7 MPOW-059
That regular expression looks syntactically wrong. If you did {.*NEW.*} then it might work. Assuming that those three letters are actually being output by bluetoothctl with no control characters mixed in. (It'd be weird to do that, but some code is weird…)
Apart from that, have you tried the diagnostic mode for expect? Pass the -d flag to the expect program when you start it to get lots of output about what it is really seeing and looking for.
So the answer appears to be:
The expect_out(buffer) is cleared by a puts statement
Find all the possible responses expected making sure that the expected response specifies the whole line.
Save the buffer in a variable if required
Issue a puts statement to clear the buffer
So:
expect {
"Hello" {
puts "$expect_out(buffer)"
exp_continue
}
-re (How.*) {
set answer $expect_out(buffer)
if {$answer == "How are you"} {
exp_send "Well thank you"
}
}
or, in the example above:
expect {
"Discovery started" {
puts $expect_out(buffer)
exp_continue
}
-re (.CHG.*) {
puts $expect_out(buffer)
exp_continue
}
-re (.NEW.*) {
set new $expect_out(buffer)

How to copy from spawned expect process into file?

I have a Raspberry Pi image running via a qemu emulator, which I interact with via expect.
I'm trying to capture the output from a particular command within the emulator, and save it to a file on the host.
Being a beginner with Tcl, I read through the manual and had a go at this. The "test.out" file is created but contains only a newline, while "Hello world!" appears on the console.
spawn qemu-system-arm --serial mon:stdio ...
expect {
"login:" { send "pi\r" }
}
expect {
"Password:" { send "raspberry\r" }
}
expect "pi#raspberrypi"
set ftty [exp_open -leaveopen]
set fsignature [open "test.out" w]
send "echo 'Hello world!'\r"
puts $fsignature [gets $ftty]
expect "pi#raspberrypi"
send "sudo shutdown now\r"
wait
I'm not familiar with exp_open. I would normally recommend something like this to capture command output:
set prompt {pi#raspberrypi}
set cmd {echo 'hello world'}
send "$cmd\r"
expect -re "$cmd\r\n(.*)\r\n$prompt"
puts $fsignature $expect_out(1,string)
Extracting command output can be tricky, because the sent command is (typically) displayed and is included in the expect output. This assumes that your specified prompt appears first in its line.
This answer was very useful in finding a solution.
However, for long outputs you need to account for the buffer filling up.
set fd [open "test.out" w]
send "cat large_output\r"
expect {
-re {cat large_output[\r\n]+} { log_user 0; exp_continue }
-ex "\n" { puts -nonewline $fd $expect_out(buffer); exp_continue }
-re $prompt { log_user 1; close $fd }
}
If the line length can exceed the buffer size then something more complicated is needed.
For some reason, the line endings are \r\r\n, but that can be fixed with a sed.
sed -i 's/\r//g' test.out

need help in eliminating race condition in my code

My code is running infinitely without coming out of loop.
I am calling expect script from shell script, that is working fine,
the problem here is script is not coming out of timout {} loop.
can someone help me in this regard.
spawn ssh ${USER}#${MACHINE}
set timeout 10
expect "Password: "
send -s "${PASS}\r"
expect $prompt
send "cmd\r"
expect $prompt
send "cmd1\r"
expect $prompt
send "cmd2\r"
expect $prompt
send "cmd3\r"
expect $prompt
send "cmdn\r"
#cmdn --> is about running script which takes around 4 hours
expect {
timeout { puts "Running ....." #<--- script is nout coming out of loop its running infinitely
exp_continue }
eof {puts "EOF occured"; exit 1}
"\$.*>" { puts "Finished.." ; exit 0}
}
The problem is that your real pattern, "\$.*>", is being matched literally and not as a regular expression. You need to pass the -re flag for that pattern to be matched as a RE, like this (I've used more lines than ; chars as I think it is clearer that way, but YMMV there):
expect {
timeout {
puts "Running ....."
exp_continue
}
eof {
puts "EOF occured"
exit 1
}
-re {\$.*>} {
puts "Finished.."
exit 0
}
}
It's also a really good idea to put regular expressions in {braces} if you can, so backslash sequences (and other Tcl metacharacters) inside don't get substituted. You don't have to… but 99.99% of all cases are better that way.

Problems looping to prompt for another password

I need some help with an EXPECT script please....
I'm trying to automate a login, prior to accessing a load of hosts, and cater for when a user enters a password incorrectly. I am getting the username and password first, and then validating this against a particular host. If the password is invalid, I want to loop round and ask for the username and password again.
I am trying this :-
(preceding few irrelevant lines omitted)
while {1} {
send_user "login as:- "
expect -re "(.*)\n"
send_user "\n"
set user $expect_out(1,string)
stty -echo
send_user "password: "
expect -re "(.*)\n"
set password $expect_out(1,string)
stty echo
set host "some-box.here.there.co.uk"
set hostname "some-box"
set host_unknown 0
spawn ssh $user#$host
while {1} {
expect {
"Password:" {send $password\n
break}
"(yes/no)?" {send "yes\n"}
"Name or service not known" {set host_unknown 1
break}
}
}
if {$host_unknown < 1} {
expect {
"$hostname#" {send "exit\r"
break
}
"Password:" {send \003
expect eof
close $spawn_id
puts "Invalid Username or Password - try again..."
}
}
} elseif {$host_unknown > 0} {
exit 0}
}
puts "dropped out of loop"
And now I can go off and do lots of stuff to lots of boxes .....
This works fine when I enter a valid username or password, and my script goes off and does all the other stuff I want, but when I enter an invalid password I get this :-
Fred#Aserver:~$ ./Ex_Test.sh ALL
login as:- MyID
password: spawn ssh MyID#some-box.here.there.co.uk
Password:
Password:
Invalid Username or Password - try again...
login as:- cannot find channel named "exp6"
while executing "expect -re "(.*)\n""
invoked from within "if {[lindex $argv 1] != ""} {
puts "Too many arguments"
puts "Usage is:- Ex_Test.sh host|ALL"
} elseif {[lindex $argv 0] != ""} {
while {1} {
..."
(file "./Ex_Test.sh" line 3)
Its the line "can not find channel named "exp6" which is really bugging me.
What am I doing wrong? I am reading Exploring Expect (Don Lines) but getting nowhere....
Whenever expect is supposed to wait for some word, it will save the spawn_id for that expect process into expect_out(spawn_id).
As per your code, expect's spawn_id is generated when it encounters
expect -re "(.*)\n"
When user typed something and pressed enter key, it will save the expect's spawn_id. If you have used expect with debugging, you might have seen the following in the debugging output
expect does "" (spawn_id exp0) match regular expression "(.*)\n"
Lets say user typed 'Simon', then the debugging output will be
expect: does "Simon\n" (spawn_id exp0) match regular expression "(.*)\n"? Gate "*\n"? gate=yes re=yes
expect: set expect_out(0,string) "Simon\n"
expect: set expect_out(1,string) "Simon"
expect: set expect_out(spawn_id) "exp0"
expect: set expect_out(buffer) "Simon\n"
As you can see, the expect_out(spawn_id) holds the spawn_id from which it has to expect for values. In this case, the term exp0 pointing the standard input.
If spawn command is used, then as you know, the tcl variable spawn_id holds the reference to the process handle which is known as the spawn handle. We can play around with spawn_id by explicitly setting the process handle and save it for future reference. This is one good part.
As per your code, you are closing the ssh connection when wrong password given with the following code
close $spawn_id
By taking advantage of spawn_id, you are doing this and what you are missing is that setting the expect's process handle back to it's original reference handle. i.e.
While {1} {
###Initial state. Nothing present in spawn_id variable ######
expect "something here"; #### Now exp0 will be created
###some code here ####
##Spawning a process now###
spawn ssh xyz ##At this moment, spawn_id updated
###doing some operations###
###closing ssh with some conditions###
close $spawn_id
##Loop is about to end and still spawn_id has the reference to ssh process
###If anything present in that, expect will assume that might be current process
###so, it will try to expect from that process
}
When the loop executes for the 2nd time, expect will try to expect commands from the spawn_id handle which is nothing but ssh process which is why you are getting the error
can not find channel named "exp6"
Note that the "exp6" is nothing but the spawn handle for the ssh process.
Update :
If some process handle is available in the
spawn_id, then expect will always expect commands from that
process only.
Perhaps you can try something like the following to avoid these.
#Some reference variable
set expect_init_spawn_id 0
while {1} {
if { $expect_spawn_id !=0 } {
#when the loop enters from 2nd iteration,
#spawn_id is explicitly set to initial 'exp0' handle
set spawn_id $expect_init_spawn_id
}
expect -re "(.*)\n"
#Saving the init spawn id of expect process
#And it will have the value as 'exp0'
set expect_init_spawn_id $expect_out(spawn_id)
spawn ssh xyz
##Manipulations here
#closing ssh now
close $spawn_id
}
This is my opinion and it may not be the efficient approach. You can also think of your own logic to handle these problems.
You simply need to store the $spawn_id as a temp variable before a nested expect command, then set the $spawn_id to the temp variable after a nested expect command.
Also, get rid of the while {1} loops. They are not needed because expect behaves like a loop provided you use exp_continue whenever you don't wish to exit. You don't need expect eof nor do you need close $spawn_id. I don't use them in the following example:
#!/usr/bin/expect
set domain [lindex $argv 0];
set timeout 300
spawn ./certbot-add.sh $domain
expect {
"*replace the certificate*" {
send "2\r";
exp_continue;
}
"*_acme-challenge*" {
puts [open output.txt w] $expect_out(buffer)
spawn ./acme-add.sh $domain
set tmp_spawn_id $spawn_id
expect {
"$ "
}
set spawn_id $tmp_spawn_id
send "\r";
exp_continue;
}
"*certificate expires on*" {
puts "Certificate Added!"
}
}

how to pack my operations into a function by using expect

I have a problem when I want to pack my operations into a function by using epext, the following are what the code looks like, the command ifconfig won't be executed when it be packed into a function, where did I do wrong, how can I pack my operations into a function? Thanks in advance.
spawn ssh x.x.x.x
proc do_sth {} {
send "ifconfig\r" # won't work
expect "~\]\#" {exit 0}
}
expect {
"*assword" { send "xxx\r"; exp_continue }
"~\]\#" { do_sth }
#"~\]\#" {
# send "ifconfig\r" # this would works fine
# expect "~\]\#" {exit 0}
#}
}
You can try sending the configurations with the spawn_id
spawn ssh x.x.x.x
#After process creation the process id will be saved in
#standard expect variable'spawn_id'
#Copying it to variable 'id'
set id $spawn_id
Now the variable 'id' is holding the reference to the ssh process. We can very well use the send and expect with the spawn id.
#Now we are setting the spawn id to our ssh process to make sure
#we are sending the commands to right process
#You can pass this variable 'id' as arg in 'do_sth'
proc do_sth { id } {
set spawn_id $id
send "ifconfig\r"
expect "~\]\#" {exit 0}
}
Or the other way around is as follows,
proc do_sth { id } {
#This way is useful, when u want to send and expect to multiple process
#simultaneously.
send -i $id "ifconfig\r"
expect -i $id "~\]\#" {exit 0}
}
As usual, you can simply call them as below.
do_sth $id