Improving an expect script to log onto whitelisted postgres instance

You can make the expect command do things conditionally. Here, you expect the password prompt, but it does not appear.

Assuming the interactive prompt is "clips-#", you can do this:

#!/usr/bin/expect
set RDSI [lindex $argv 0]
spawn /usr/local/bin/psql -U postgres -h $RDSI -d clips
set prompt "clips-#"
expect {
    "Password" 
        send "myPassword\r" 
        exp_continue
    }
    $prompt
}
interact 

There are 2 patterns in that expect statement. If “password” is seen, it will send the password and then continue in that same expect statement. When the prompt is seen, since there is no action for it, the expect command returns.

For the last line of an expect script, use

  • interact if you, the human, needs to interact with the spawned process, or
  • expect eof if no interaction is required, you just need to wait until the process ends.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top