LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   how to ssh in two machine one after another (in sequence) using shell script (https://www.linuxquestions.org/questions/linux-networking-3/how-to-ssh-in-two-machine-one-after-another-in-sequence-using-shell-script-676161/)

kushkothari 10-13-2008 09:44 PM

how to ssh in two machine one after another (in sequence) using shell script
 
I want to know how can i ssh to two machine one inside another using shell script
example
first i have to ssh to the host
then from host i have ssh to a node(say some another host inside first host)


/* some shell script command*/
I have shell script in which i have to ssh to these two hosts one after another run a program and come out of the host
/* some shell script command */

jb_get 10-13-2008 09:58 PM

I dont think that is possible just because once you log in the first shell you lose the shell you were just in, but maybe im wrong, curious to find out.

billymayday 10-13-2008 10:26 PM

You should be able to get your remote environment to automatically run execute a command when you log in. Make that command an ssh command and you should be done.

I'll let you work out the details.

kushkothari 10-13-2008 10:26 PM

you are right I am facing exactly the same problem but I hope someone must know how to do it

kushkothari 10-13-2008 10:28 PM

can you explain me more....


Quote:

Originally Posted by billymayday (Post 3309282)
You should be able to get your remote environment to automatically run execute a command when you log in. Make that command an ssh command and you should be done.

I'll let you work out the details.


billymayday 10-13-2008 10:34 PM

I'd set up a user, say relay1

In CentOS, I'd edit the user's .bash_profile with a line like
Code:

ssh user@target_machine
Set up ssh keys so you don't need to enter a password and I'd imagine you'll be done.

billymayday 10-13-2008 10:36 PM

I just ran a very quick test and this approach would seem to work

kushkothari 10-13-2008 10:42 PM

I have ssh key but I don't know how to use it
i mean where i have to store in my computer and how?
then how to use it for remote login?
could you please tell me this thing too..

billymayday 10-13-2008 10:46 PM

Read man ssh-keygen

It's not that hard

kushkothari 10-14-2008 12:20 AM

I can't do it
as I login to server(my account) and then every time I have to ssh to my nodes (node0,node1,......) so if i keep a command to ssh to one node then what about the other nodes?

kushkothari 10-14-2008 12:20 AM

i have done the ssh key part now i m able to ssh to server without any password

billymayday 10-14-2008 01:20 AM

Well my way would be to create a user per node on the server and have a different ssh command in the profile.

billymayday 10-19-2008 11:47 PM

I have a somewhat neater solution for this problem now. I couldn't get it to work properly previously, but had a need to use it myself, so spent some time sorting it out.

Step one: Set up keys on the intermediate machine (call this "server") so that user@server can ssh to the node without needing a password (ie, from account user on server, ssh user@node is passwordless).

Step two: ssh to server forcing pseudo-terminal creation, and passing the final ssh command to ssh

Code:

$ ssh -t user@server ssh user@node
I'd been trying the -t on the second invocation on ssh (ie ssh user@server ssh -t user@node) which doesn't work properly.

kushkothari 10-21-2008 12:54 AM

Thanks a lot
 
it's working fine

chort 10-21-2008 08:50 AM

There's a better way to do this:

1. Generate key (with a passphrase) on your workstation using ssh-keygen

2. Start ssh-agent

3. Add key to agent with ssh-add

4. Edit ~/.ssh/config on workstation to add this:
PHP Code:

Host server.domain.com server
    ForwardAgent yes 

5. scp your key to the server and each node. Place it in ~/.ssh/authorized_keys

6. Now you can do ssh user@server ssh user@node1 some_command

The advantage is that if someone compromises your account on server, they don't automatically get free access to each node.


All times are GMT -5. The time now is 11:38 AM.