Expect and interact

I am trying to log on to server as a normal user and then sudo to root.

I am using below expect script.

[linux@linux]$ cat SC.orig 
expect <<EOF # | tee -a $LOGFILE 
spawn sshpass -p "mypassword" ssh -l myid  nim
expect "$"
send  "sudo su - root\n"
expect "Password:"
send "mypassword\n";
sleep 4
interact 
[linux@linux]$ 

When I run this .. I get below

[linux@linux]$ SC.orig 
/home/linux/bin/SC.orig: line 9: warning: here-document at line 1 delimited by end-of-file (wanted `EOF')
spawn sshpass -p mypassword ssh -l myid nim
sudo su - root
mypassword
Last unsuccessful login: Fri Jan  9 09:44:23 2015 on ssh from abcxyx
$ sudo su - root
mypassword
Password:
TERM=xterm
<<< 
[nim:/home/root] [linux@linux]$ 

This script is authenticating me to remote server and then doing sudo to root but then exits from shell and comes back to my local shell.

I want my script to enter passwords and then take me to shell.

Could you please help me with this script ?

You are using a here-document but there is no closing EOF.
When you say <<EOF...there should be an EOF to close the here-document.

Shell does not work that way. Expect does not work that way.

You would have to write an expect program which read input from the keyboard and fed it into the remote shell.

It would be much better for you to arrange sudo to allow your user to su without a password. Then you could do:

ssh -t username@host exec sudo su -

...and it would do it.

For ssh, you can use keys instead of sshpass. sshpass is very dangerous, it displays the password in ps for anyone to see.

1 Like

Yes, this is working for me. Thank you