3

I have been trying to automatically enter a ssh connection using a script. This previous SOF post has helped me so far. Using one connection works (the first ssh statement). However, I want to create another ssh connection once connected, which I thought could look like this:

#! /bin/bash
# My ssh script

sshpass -p "MY_PASSWORD1" ssh -o StrictHostKeyChecking=no *my_hostname_1*
sshpass -p "MY_PASSWORD2" ssh -o StrictHostKeyChecking=no *my_hostname_2*

When running the script, I get only connected to the my_hostname_1 and the second ssh command is not run until I exit the first ssh connection.

I've tried using an if statement like this:

if [ "$HOSTNAME" = my_host_name_1 ]; then
    sshpass -p "MY_PASSWORD2" ssh -o StrictHostKeyChecking=no *my_hostname_2*
fi

but I can't get any commands to be read until I exit the first connection.

Community
  • 1
  • 1
solalito
  • 1,189
  • 4
  • 19
  • 34
  • 1
    I mean, how is it supposed to work? You can't have two active SSH sessions in the same window (despite e.g. by splitting the pane). What exactly are you trying to do? – Tim Zimmermann Sep 03 '14 at 12:01
  • 1
    if you're hoping to send the same cmd (at the same time) to multiple computers, this is not the solution. (There is a sysadmin program that will do this, but I can't find the name right now). Otherwise, read up about running shell cmds in the background with the `&` char at the end of a cmd-line. Good luck. – shellter Sep 03 '14 at 12:03
  • 1
    @TimZimmermann: he probably wants to use the first host as a gateway to the second. – salva Sep 03 '14 at 12:05
  • @TimZimmermann The my_hostname_2 is only accessible throught my_hostname_1. Without a script I would run the first `ssh` command to connect to my_hostname_1, then run the second `ssh` command to connect to my_hostname_2. I thought I'd write a script in order to save time. – solalito Sep 03 '14 at 12:07
  • 3
    check ProxyCommand directive in ssh_config – lihao Sep 03 '14 at 12:29

2 Answers2

3

Here is a ProxyCommand example as suggested by @lihao:

#!/bin/bash

sshpass -p "MY_PASSWORD2" ssh -o StrictHostKeyChecking=no \
    -o ProxyCommand='sshpass -p "MY_PASSWORD1" ssh m_hostname_1 netcat -w 1 %h %p' \
    my_hostname_2

You are proxying through the first host to get to the second. This assumes you have netcat installed on my_hostname_2. If not, you'll need to install it.

You can also set this up in your ~/.ssh/config file so you don't need the proxy stuff on the command line:

Host my_hostname_1
    HostName my_hostname_1

Host my_hostname_2
    HostName my_hostname_2
    ProxyCommand ssh my_hostname_1 netcat -w 1 %h %p

However, this is a little trickier with the password handling. While you could put the sshpass here, it's not a great idea to have passwords in plain text. Using key based authentication might be better.

zerodiff
  • 1,690
  • 1
  • 18
  • 23
  • 2
    With the latest versions of `ssh` you can avoid calling `netcat`: `ProxyCommand ssh my_hostname_1 -W %h:%p` (it requires tunnels enabled on the gateway, through). – salva Sep 18 '14 at 08:19
0

A Bash script is a sequence of commands.

echo moo
echo bar

will run echo moo and wait for it to complete, then run the next command.

You can run a remote command like this:

ssh remote echo moo

which will connect to remote, run the command, and exit. If there are additional commands in the script file after this, the shell which is executing these commands will continue with the next one, obviously on the host where you started the script.

To connect to one host from another, you could in principle do

ssh host1 ssh host2

but the proxy command suggested by @zerodiff improves on several aspects of the experience.

tripleee
  • 175,061
  • 34
  • 275
  • 318