I have written an expect script to automate the login process for ssh. My user account has rsa keys setup but once I log in I want to switch to root but the remote server is hanging on the password. I can see the script feeding the password to the remote server, albeit its visible, which is another thing I'd like to know if I can hide. But can someone tell me why this is hanging?


spawn ssh <server> su - 
expect "Password"
send "MyPassword\r"

It just sits there at this screen:

Password: "password"

With the cursor right below it which blinks continously. In /var/log/secure I have compared the lines of my scripted login attempt with a normal one where I manually get root.

Script su:

Jun  7 15:17:50 <server> su: pam_unix(su-l:session): session opened for user root by (uid=1000)

Manual su:

Jun  7 15:18:48 <server> su: pam_unix(su-l:session): session opened for user root by <user>(uid=1000)

The only thing is that I see different is the manual login has my username listed.

I have tried changing up the command to :

spawn ssh <server> su - root
spawn ssh <server> sudo su

But neither one works. How can I get this going?

1 Answer 1


In the script I had to pass it the -t flag to force tty allocation:

spawn ssh -t <server> su -

It now works as expected.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .