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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
07-21-2012, 04:43 AM
|
#1
|
LQ Newbie
Registered: Jul 2012
Location: India
Posts: 6
Rep: 
|
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
|
|
|
07-21-2012, 11:56 AM
|
#2
|
Senior Member
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350
Rep: 
|
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").
|
|
|
07-21-2012, 12:18 PM
|
#3
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep: 
|
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.
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.
|
|
|
07-21-2012, 12:24 PM
|
#4
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep: 
|
You can also use cssh(clusterssh).
|
|
|
07-21-2012, 10:47 PM
|
#5
|
LQ Newbie
Registered: Jul 2012
Location: India
Posts: 6
Original Poster
Rep: 
|
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?
|
|
|
07-22-2012, 02:10 AM
|
#6
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep: 
|
Quote:
Originally Posted by iwlu212
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.
|
|
|
07-22-2012, 10:56 AM
|
#7
|
LQ Newbie
Registered: Jul 2012
Location: India
Posts: 6
Original Poster
Rep: 
|
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"
|
|
|
07-22-2012, 02:30 PM
|
#8
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep: 
|
Quote:
Originally Posted by iwlu212
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.
|
|
|
07-26-2012, 01:05 PM
|
#9
|
LQ Newbie
Registered: Jul 2012
Location: India
Posts: 6
Original Poster
Rep: 
|
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
|
|
|
07-27-2012, 05:45 AM
|
#10
|
Senior Member
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386
Rep: 
|
Quote:
Originally Posted by iwlu212
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
|
|
|
07-27-2012, 08:05 AM
|
#11
|
LQ Newbie
Registered: Jul 2012
Location: India
Posts: 6
Original Poster
Rep: 
|
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 05:03 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|