2

Below is a sample snippet, normally it works fine unless we passes some command which shutdown the server or cause a disconnection .

To handle this, is it possible to expect either "#" or eof after sending the cmd .

  send "${_cmd_}\r"
     expect "#"
      send "exit\r"
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Bharat
  • 814

1 Answers1

2

Sure, if the connection closes, expect will react to the special pattern eof. What you want to do is this

  • if the connection is closed, expect eof
  • if you see the prompt, send the exit command, then continue to wait for eof
send "$_cmd_\r"
expect {
    "#" {
        send "exit\r"
        exp_continue
    }
    eof
}
# do something after the connection is closed
Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
glenn jackman
  • 85,964