How to suppress expect send output?

后端 未结 1 395
一生所求
一生所求 2020-12-10 17:47

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

相关标签:
1条回答
  • 2020-12-10 18:26

    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

    0 讨论(0)
提交回复
热议问题