Aeronaelius
Aeronaelius

Reputation: 65

Script to automate two consecutive ssh connections

I know it is possible to write a shell script which passes your hard-coded password to a ssh connection authentication (using expect). However what I need is slightly more complicated.

At my university I have a desktop computer appointed to me. I can connect remotely to this computer by first making a ssh connection with some server, then making another ssh connection from that server to my appointed desktop computer. This goes like:


localuser@localcomputer:~$ ssh -X username@serveraddress

username@serveradress password:

server$ ssh -X username@remotecomputeraddress

username@remotecomputeraddress password:

username@remotecomputer:~>


Is there a way to write a script which could automate the above (i.e. performing two consecutive ssh connections)?

Thanks in advance!

ps: Both the local and the remote computers are running on Linux.

Upvotes: 2

Views: 1876

Answers (3)

dstromberg
dstromberg

Reputation: 7187

I wrote something to do this with bang paths a while back: http://stromberg.dnsalias.org/~strombrg/deep-ssh.html

So you'd set up passwordless, passphraseless authentication (or use an agent for the passphrase), like: http://stromberg.dnsalias.org/~strombrg/ssh-keys.html

And then:

deep-ssh username@serveraddress!username@remotecomputeraddress command

If bash complains about the !, you can just escape it with a backslash.

The old timers will recognize that this is how UUCP paths were specified.

Upvotes: 0

twalberg
twalberg

Reputation: 62499

You can do this interactively with:

ssh -t -X username@serveraddress ssh -t -X username@remotecomputeraddress

Note that is not a pipe - the second ssh is the command to run on the connection created by the first ssh. The -t options are necessary to allocate the pseudo-ttys necessary for interaction (password gathering as well as the ultimate goal - an interactive session on the remote system). Wrapping it up with expect left as an exercise for the reader.... ;-)

Bonus points for setting up proper private/public key pairs and ssh-agent so that the passwords aren't necessary (unless, of course, that is disallowed for security reasons).

Upvotes: 1

Igor Chubin
Igor Chubin

Reputation: 64613

Yes, you can do this.

Presuming you have your except script in the expect_script:

cat expect_script | ssh -X username@serveraddress sh -s

In this expect_script you must run ssh -X username@remotecomputeraddress.

And of course you can install public keys on the both hosts and use passwordless authentication.

Upvotes: 0

Related Questions