Ubuntu – how to use expect with bash

bashcommand linescriptsssh

I Have created a shell script as given below.This script will login to a remote server as a normal user then switch to root user for creating a directory.The script is as given below.

ssh -t qbadmin@10.3.2.0 '
su root -c "
echo \"Give Directory name :\";
read dir;
mkdir \$dir;
";
'

Here the script will ask Password for normal user first.Then again it will ask for root Password.How could I automate this using expect command.I want to supply the password automatically for the root user only.I think it can be done with expect.

Please do help me.

Best Answer

I don't see an accepted answer years later so I will also add something that may be helpful to others. My situation had nothing to do with SSH, but using spawn expect in the following way I was able to get it working.

expect -c "  
  spawn myProc
  expect \"Do you wish to continue with XYZ? (y/N)\" {
    send \"n\r\"
    exp_continue
  }          
"

Hope this helps.

Related Question