LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-21-2012, 04:43 AM   #1
iwlu212
LQ Newbie
 
Registered: Jul 2012
Location: India
Posts: 6

Rep: Reputation: Disabled
Shell Script for Invoking SSH


I am new to shell scripting .I want to take help from all the experts here .

I want to write a shell script (to be run on linux machine) to invoke 'n' numbers of ssh window and execute commands on these ssh windows at once .Please provide your expert help
 
Old 07-21-2012, 11:56 AM   #2
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
If I understand you right, all you need to do is put lines in a bash-file like this:
#!/bin/bash
ssh user@server1 <command> &
ssh user@server2 <command> &

The commands will simply be executed, but you won't get new ssh windows. Do you really need that?

Then of course, if you don't want to type in password you need to configure ssh to login with certificate (aka "key").
 
Old 07-21-2012, 12:18 PM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
You have to setup password less ssh setup for each machine.

If you are using OpenSSH, then it may not possible to run the commands at the same time on 'n' number of ssh window, as you have to relogin again and again in a loop to execute commands on each.

Instead you can use pssh(parallel ssh) to run your command parallelly on each machine.

e.g.

Quote:
pssh -h <host-file> date
Here you can run date command parallely on each hosts present in the file <host-file>

see the manual for different options.

Last edited by divyashree; 07-21-2012 at 12:19 PM.
 
Old 07-21-2012, 12:24 PM   #4
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
You can also use cssh(clusterssh).
 
Old 07-21-2012, 10:47 PM   #5
iwlu212
LQ Newbie
 
Registered: Jul 2012
Location: India
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi Divyashree and Pingu thanks for your reply !!

It is required for me to open new windows and the commands gets executed on these windows as i have to watch and go through the logs on each
ssh windows .

@Divyashree : I don't have pssh installed on my client machine.I first want to know if we can automate my requirement using shell/perl script


To make things clear here is what i want to do

Run a script which opens up 'n' number of ssh windows and then on each the required command gets executed on parallel . Is this possible?
 
Old 07-22-2012, 02:10 AM   #6
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by iwlu212 View Post
Hi Divyashree and Pingu thanks for your reply !!

It is required for me to open new windows and the commands gets executed on these windows as i have to watch and go through the logs on each
ssh windows .

@Divyashree : I don't have pssh installed on my client machine.I first want to know if we can automate my requirement using shell/perl script


To make things clear here is what i want to do

Run a script which opens up 'n' number of ssh windows and then on each the required command gets executed on parallel . Is this possible?
Dear iwlu212 without using pssh/cssh , the commands cant be executed at once/parallel, but one after another.

What you need can be changed as : open 'n' numbers of windows/terminals and then run ssh in each to do something.

You can use like this in your script to open a new terminal and run some command in them:

Code:
gnome-terminal -e <your-ssh-to-othermachine-to-run-some-command>
or
Quote:
konsole --noclose -e <your-ssh-to-othermachine-to-run-some-command>
If you will run them in background mode, you cant see the the outputs.

Last edited by divyashree; 07-22-2012 at 02:17 AM.
 
Old 07-22-2012, 10:56 AM   #7
iwlu212
LQ Newbie
 
Registered: Jul 2012
Location: India
Posts: 6

Original Poster
Rep: Reputation: Disabled
Divya thanks again for your reply .I tried the command but got an error can you please correct it .

gnome-terminal -e ssh root@x.x.x.x

Invalid argument: "root@x.x.x.x"
 
Old 07-22-2012, 02:30 PM   #8
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by iwlu212 View Post
Divya thanks again for your reply .I tried the command but got an error can you please correct it .

gnome-terminal -e ssh root@x.x.x.x

Invalid argument: "root@x.x.x.x"
Use quote:
e.g.
Code:
gnome-terminal -e "ssh root@localhost sleep 1 && ls -lrth && sleep 1"

Last edited by divyashree; 07-22-2012 at 02:58 PM.
 
Old 07-26-2012, 01:05 PM   #9
iwlu212
LQ Newbie
 
Registered: Jul 2012
Location: India
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hi Divya !! yes the below option works but after the command execution the ssh terminal gets killed , i want it to remain open and not get killed

I am using it gnome-terminal -e "ssh user$hostname command"
What modification is required for the command to get excuted and window not getting closed

Thnx Again
 
Old 07-27-2012, 05:45 AM   #10
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,363

Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by iwlu212 View Post
Hi Divya !! yes the below option works but after the command execution the ssh terminal gets killed , i want it to remain open and not get killed

I am using it gnome-terminal -e "ssh user$hostname command"
What modification is required for the command to get excuted and window not getting closed

Thnx Again
You can ask the terminal to wait for something to read :

Code:
gnome-terminal -e "ssh localhost command; read line"
Then you have to press enter to close
 
Old 07-27-2012, 08:05 AM   #11
iwlu212
LQ Newbie
 
Registered: Jul 2012
Location: India
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thumbs up

Thnx Divya !! yes now it is not getting closed but i am stuck in another problem .. i am using it as mentioned below

gnome-terminal -e "ssh host_name@host for i in `seq 1 2` ; do echo hello_$i;done;read line"

but the output i am getting is

hello_
hello_

and i want it to be
hello_1
hello_2

the value of variable i is not getting interpreted . how can we make this work

Thanks Again
 
  


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 do I make a script to export its variables to the invoking shell? stf92 Linux - Newbie 7 10-06-2011 07:25 AM
Invoking java from a bash terminal via shell script fails under Mint Debian rizwanjavaid Programming 3 02-16-2011 01:36 PM
invoking windows command inside a shell script nano2 Programming 6 09-09-2009 03:46 AM
Invoking a shell script remotely. niks27 Linux - Newbie 1 10-11-2007 07:22 AM


All times are GMT -5. The time now is 08:09 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration