I am seeking to automate ssh password based logins (and a series of actions after logging in).
I am aware that the ssh password prompt bypasses STDIN. To that end I put together a quick expect script.
spawn ssh -o StrictHostKeyChecking=No $USERNAME@$HOST
expect {
timeout { send_user "\nFailed to get password prompt\n"; exit 1 }
eof { send_user "\nSSH failure for $HOST\n"; exit 1 }
"*assword"
}
send "Pasword123\r"
expect {
timeout { send_user "\nLogin failed. Password incorrect.\n"; exit 1}
"*\$ "
}
sleep 1
send "echo 002-READY\r"
interact
This appeared to work as I expected. But when I feed further commands into STDIN of the running script after 'interact' they don't seem to arrive in the ssh session, e.g.
$ cat feed.sh
#!/bin/bash
sleep 3; echo "cat /etc/hosts"
$ ./feed.sh | ./ssh_expect_script
However it does detect the EOF and terminates the session.
(Please don't tell me the solution is to use key-pairs; there are reasons why interactive passwords are a specific constraint.)
How do I get input routed via Expect to the remote session?
Alternatively, how can I send the password to ssh?
(Regarding the alternate question, I'm using PHP as the controlling mechanism. As a last resort, I could dynamicaly generate the whole expect script. I tried writing to the tty directly, but that just comes back on my screen. I also looked at ssh-askpass, but the documentation only mentions passphrases / I can't find a version that doesn't rely on have a desktop environment running).