hello, I wrote an expect script to automate ssh commands, and it was working, but for some reason it's not anymore. Just to make sure I didn't do any weird changes that I didn't notice, I tried several of the dozens of such scripts that are online, and for some reason, none of them work. Here's the script I wrote, that did work at one time, but maybe there are some suggestions, I'm not the best at shell scripting.

I wrote this to work with ssh as well as rsync, so $1 is the command you want it to run, with $2 being the password, example of calling this would be: "./shell-script 'user@server.com ls' 'password'"

I am using ubuntu linux.


expect -c "
spawn $1
expect {
password: { send $2; exp_continue }

exit 0

I should add, that this script will call the command ($1), but it will hang when it comes to entering the password. I've tried several different strings for it to expect, none of them work. The format ssh outputs for asking the password is 'user@server.com's password:' so I've tried 'password:', 'word:' and ':'


I missed that in my proofreading of my post, yes I intended to say that initially. Here is an example of my script running (input & output):

I type in "./shell-script 'ssh user@sever.com' 'password'" and hit enter

it outputs "spawn ssh user@server.com"
then it outputs ssh's output "user@server.com's password:"
and it stays there for a few seconds til ssh times out on the password and it exits.


This article has been dead for over six months. Start a new discussion instead.