Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress expect send output?

Tags:

expect

I have an expect script which I'd like to behave as a fancy ssh program that hops several machines and sets up the environment on target machine before running the commands.

I can use log_user 0/1 to turn off / on output from expect and that helps with password prompts and login banners, and commands to setup environment.

But, like ssh, once my script starts to issue commands, I don't want to see the issued command. That is I don't want to see "command" after send "command\n". All I want to see is the results of command.

How do I suppress the send output, but not the results?

Here's a snippet of the expect script:

log_user 1
foreach daline [lrange \$argv 0 end] {
   send "\$daline\r"
   set buffer1 
}

So prior to this loop, I send password, and setup environment. Then in this loop, I run each bash command that was fed to the expect as an argument.

thanks.

like image 891
Bitdiot Avatar asked Nov 07 '14 14:11

Bitdiot


1 Answers

Many programs echo their input. For example, if you send the date command to the shell, you will see the string date followed by a date. More precisely, you will see everything that you would ordinarily see at a terminal. This includes formatting, too.

send "date\r" 
expect -re $prompt 

The command above ends with expect_out (buffer) set to date\r\nFri Nov 7 20:47:32 IST 2014\r\n. More importantly, the string date has been echoed. Also, each line ends with a \r\n, including the one you sent with a \r. The echoing of date has nothing to do with the send command.

To put this another way, there is no way to send the string and have send not echo it because send is not echoing it in the first place. The spawned process is.

In many cases, the spawned process actually delegates the task of echoing to the terminal driver, but the result is the same-you see your input to the process as output from the process.

Often, echoed input can be handled by using log_user only (which you have used in different place). As an example, suppose a connection to a remote host has been spawned and you want to get the remote date, but without seeing the date command itself echoed. A common error is to write:

log_user 0 ;# WRONG 
send "date\r" ;# WRONG 
log_user 1 ;# WRONG 
expect -re .*\n ;# WRONG

When run, the log_user command has no effect because expect does not read the echoed "date" until the expect command. The correct way to solve this problem is as follows:

send "date\r" 
log_user 0 
expect -re "\n(\[^\r]*)\r" ;# match actual date 

log_user 1 
puts "$expect_out(l,string)" ;# print actual date only 

If you are sending a lot of commands to a remote shell it may be more convenient to just disable all echoing in the first place. You can spawn a shell and then send the command stty -echo, after which your commands will no longer be echoed. stty echo re enables echoing.

spawn ssh <host>
stty -echo; # Disable 'echo' here
expect something

#Your further code here

stty echo # Enable 'echo' here

#Close of connection

Reference : Exploring Expect

like image 66
Dinesh Avatar answered Nov 10 '22 03:11

Dinesh