LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell Script for Invoking SSH (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-for-invoking-ssh-4175417929/)

iwlu212 07-21-2012 04:43 AM

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

pingu 07-21-2012 11:56 AM

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").

divyashree 07-21-2012 12:18 PM

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.

divyashree 07-21-2012 12:24 PM

You can also use cssh(clusterssh).

iwlu212 07-21-2012 10:47 PM

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?

divyashree 07-22-2012 02:10 AM

Quote:

Originally Posted by iwlu212 (Post 4734883)
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.

iwlu212 07-22-2012 10:56 AM

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"

divyashree 07-22-2012 02:30 PM

Quote:

Originally Posted by iwlu212 (Post 4735189)
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"

iwlu212 07-26-2012 01:05 PM

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

divyashree 07-27-2012 05:45 AM

Quote:

Originally Posted by iwlu212 (Post 4738766)
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

iwlu212 07-27-2012 08:05 AM

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


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