How to wait for a process to complete using tcl-expect

后端 未结 4 2105
小蘑菇
小蘑菇 2021-02-15 18:12

I am writing a script using expect in which I have to rlogin to some host & after that I need to send some commands. Now I want to exit to that host and relogin again to som

相关标签:
4条回答
  • 2021-02-15 18:27

    Try this construct when command is time consuming:

    . . .
    send "command\r"
    
    expect {
        timeout {
            puts "Running..."
            exp_continue
        }
        "%PROMPT%" {
            puts "Finished."
        }
    }
    
    send "next command\r"
    . . .
    

    On timeout you will be continuosly waiting with exp_continue command for %PROMPT%.

    0 讨论(0)
  • 2021-02-15 18:30

    I found this post after I ran into a race condition problem: sometimes the script completed as expected and other times it didn't. Adding a catch statement to the script ensured the desired outcome every time:

    [...]
    expect eof
    catch wait result
    
    0 讨论(0)
  • 2021-02-15 18:36

    Can you elaborate on "still script sending rest of the commands in between the previous process. "

    I tried this and it worked.

    spawn ssh host1
    expect ":"
    send pwd1
    expect "%"
    send "echo hi\r"
    expect "%"
    send "exit\r"
    expect eof
    spawn ssh host2    
    expect ":"
    send pwd2
    
    0 讨论(0)
  • 2021-02-15 18:42

    you're forgetting to "hit enter". After sending exit, the way to wait for the process to end os expect eof:

    send "source xyz.csh\r"
    expect "%"
    send "exit\r"
    expect eof
    
    0 讨论(0)
提交回复
热议问题