To login into my database the following are the steps I follow every time:

ssh [email protected] 

Once I am connected, I log in into a local database server in the same terminal:

ssh database_name 

Then I do an su as:

su - appuser

And next I do the actual connection to sqlplus:

sqlplus ..

I am trying to automate this using a unix script, but I am not able to figure out the following:

  1. how do I use 2 SSH in the same script
  2. I could go as far as "ssh database_name" and entered the password, but then control returns to the terminal and rest of the script is not executed
  3. I understand that after the second ssh I have to pass in the rest of the commands, starting with the su command as a parameter; but I don't know how to do the same.
  • you have to configure ssh-keygen so that you can login.
    – AReddy
    Mar 8, 2016 at 17:06
  • Are you using passwordless login (i.e. using keypairs with empty passphrases) or are you trying to hardcode passwords into the scripts ? Or you are just typing the password as they get asked ?
    – MelBurslan
    Mar 8, 2016 at 17:07
  • Thank you for your comment :) I am typing the password as they are asked. I have to do this as the passwords keep getting changed often and this script will be used by people having different unix ids and passwords; so I am not sure if I can use ssh-keygen. Mar 8, 2016 at 17:40

2 Answers 2


My approach to this would be two-fold. One, configure password-less keyed login for SSH via ssh-keygen as @Avinash mentions in a comment. Second, make use of expect to step through the two SSH sessions and the launch of sqlplus, then return control to the terminal.

  • Thank you John. Can you give a small example for this, since I am new to unix and not sure about how to use the expect to step through the SSH sessions. I did try to use the expect command and using expect *$ and then giving the password, but I was getting authentication failed everytime. Mar 8, 2016 at 17:43
  • It would be helpful if you could please put an example or point me someplace where this is already answered. Mar 8, 2016 at 17:45

Something like this, plus passphrase-less pubkey auth should do the trick:

# create an ssh connection for tunneling only, in the background (this assumes tunneling is allowed... usually is)
ssh -N -L 9999:database_name:22 [email protected] &

# connect to the 2nd machine directly, using the tunnel, also run only the sqlplus command. (this assumes sudo is installed)
ssh -oPort 9999 localhost sudo -u appuser sqlplus ..

kill $pid

The first command can be replaced by a special config in ~/.ssh/config. Here's an example:

Host mytunnel
    User unix_id
    Hostname something.com
    LocalForward 9999 localhost:22

Host sqlviatunnel
    User unix_id
    Hostname localhost
    Port 9999
    ProxyCommand ssh -q -W %h:%p mytunnel

And then

ssh sqlviatunnel

Rename sqlviatunnel to something shorter.

You must log in to answer this question.

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