Using #!/usr/bin/expect and bash

Tag: bash , expect Author: yuyuyuyuyu007 Date: 2014-02-08

Is there any way I can use bash script along with /usr/bin/expect commands?

Could I, for instance, use an if statement in a usr/bin/expect script?

zero effort? google for expect language if.
If you really want bash, as opposed to TCL, then I'd suggest using emPTY instead of Expect: empty.sf.net

Best Answer

expect is an extension of tcl - therefore you can use the programming constructs of tcl - these include 'if'.

Tcl command man pages are available: tcl.tk/man/tcl8.5/TclCmd/contents.htm

comments:

Tcl command man pages: tcl.tk/man/tcl8.5/TclCmd/contents.htm
Thanks - have added reference to the answer

Other Answer1

Here is a very simple script that connects to a server via ssh from bash:

#!/bin/bash

/usr/bin/expect <<EOF
    spawn ssh [email protected]
    expect {
        "assword:" {
            send -- "1234\r"
        }
    }
EOF

comments:

I would really recommend reading Exploring Expect by Don Libes
Note that it simply connects, then the ssh session dies when the expect script ends: it does not leave you connected to the server when the bash script resumes.