LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices



Reply
 
Search this Thread
Old 10-13-2008, 10:44 PM   #1
kushkothari
Member
 
Registered: Oct 2008
Posts: 31

Rep: Reputation: 15
Question 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 */
 
Old 10-13-2008, 10:58 PM   #2
jb_get
Member
 
Registered: Sep 2008
Location: America
Distribution: Debian
Posts: 33

Rep: Reputation: 16
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.

Last edited by david_ross; 09-17-2010 at 10:56 AM. Reason: removed spam
 
Old 10-13-2008, 11:26 PM   #3
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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.
 
Old 10-13-2008, 11:26 PM   #4
kushkothari
Member
 
Registered: Oct 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Unhappy

you are right I am facing exactly the same problem but I hope someone must know how to do it
 
Old 10-13-2008, 11:28 PM   #5
kushkothari
Member
 
Registered: Oct 2008
Posts: 31

Original Poster
Rep: Reputation: 15
can you explain me more....


Quote:
Originally Posted by billymayday View Post
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.
 
Old 10-13-2008, 11:34 PM   #6
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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.
 
Old 10-13-2008, 11:36 PM   #7
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
I just ran a very quick test and this approach would seem to work
 
Old 10-13-2008, 11:42 PM   #8
kushkothari
Member
 
Registered: Oct 2008
Posts: 31

Original Poster
Rep: Reputation: 15
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..
 
Old 10-13-2008, 11:46 PM   #9
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Read man ssh-keygen

It's not that hard
 
Old 10-14-2008, 01:20 AM   #10
kushkothari
Member
 
Registered: Oct 2008
Posts: 31

Original Poster
Rep: Reputation: 15
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?
 
Old 10-14-2008, 01:20 AM   #11
kushkothari
Member
 
Registered: Oct 2008
Posts: 31

Original Poster
Rep: Reputation: 15
i have done the ssh key part now i m able to ssh to server without any password
 
Old 10-14-2008, 02:20 AM   #12
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Well my way would be to create a user per node on the server and have a different ssh command in the profile.
 
Old 10-20-2008, 12:47 AM   #13
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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.
 
Old 10-21-2008, 01:54 AM   #14
kushkothari
Member
 
Registered: Oct 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Thumbs up Thanks a lot

it's working fine
 
Old 10-21-2008, 09:50 AM   #15
chort
Senior Member
 
Registered: Jul 2003
Location: Silicon Valley, USA
Distribution: OpenBSD 4.6, OS X 10.6.2, CentOS 4 & 5
Posts: 3,660

Rep: Reputation: 69
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 07:24 AM
running shell command on remote machine with ssh qrshat Solaris / OpenSolaris 3 08-17-2006 08:32 AM
Simple shell script to open up terminal and ssh to another machine deadseasquirrel Programming 1 02-02-2006 06:25 AM
telneting remote machine from a shell script sanjith11 Programming 11 05-26-2004 07:50 AM
Ssh or logging from shell script Alek Linux - General 5 09-24-2003 11:30 AM


All times are GMT -5. The time now is 01:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration