How to suppress pending submission?

I have a pending script that I would like to run like a fantastic ssh program that hijacks multiple machines and sets up the environment on the target machine before running commands.

I can use log_user 0/1 to turn off / on from expected and it helps with password hints and login banners and commands to set up the environment.

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

How do I suppress send output but not results?

Here is a snippet of the expected script:

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

      

So, before this loop, I am submitting the password and environment setup. Then in that loop, I run each bash command that was pending as an argument.

thank.

+3


source to share


1 answer


Many programs repeat their input. For example, if you send a command date

to the shell, you will see the line date followed by the date. More precisely, you will see everything that you usually see on the terminal. It's also formatting.

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 line date has been repeated. Also, every line ends with \r\n

, including the one you posted with \r

. The countdown has nothing to do with the command send

.

To do it differently, there is no way to send a string and send a non-echo, because send doesn't echo it in the first place. The resulting process.

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

Often the echo input can only be handled with log_user

(which you have used elsewhere). As an example, let's say a connection to a remote host has been made and you want to get the remote date, but not seeing that the date command itself is echoed. A common mistake is to write:



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

      

On startup, the command has log_user

no effect because it expect

does not read the echo "date" before the command expect

. 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 large number of commands to the remote shell, it may be more convenient to just turn off all echoes first. You can create a shell and then send a command stty -echo

, after which your commands will no longer be displayed. stty echo

re enables echo.

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

#Your further code here

stty echo # Enable 'echo' here

#Close of connection

      

Ref: Exploring Expectations

+8


source







All Articles