LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2014, 01:41 PM   #1
zaibrockstar
LQ Newbie
 
Registered: May 2014
Posts: 2

Rep: Reputation: Disabled
Cannot SSH into multiple servers with this script


Hi,

I am trying to get few parameters from all the servers I have specified in a file.

I have tried to write a script. But It doesn't go to all the servers. I have public private keys on all the servers already set. I don't want to install any other program for doing it.


Can any one suggest me why its not working. It prints the server names in the list but its not logging to the server for getting the parameters. It just prints the parameters from the server where the script is running.

Quote:
#! /bin/bash

FILE1="/export/home/servers.list"

Count=1
for Server in $( cat $FILE1 ); do

ssh -t $Server
KR=`rpm -qa | grep kernel`
LR=`last reboot`
UT=`uptime`
OS=`cat /etc/redhat-release`
Version=`uname -i `

echo $Server
echo -e "$OS \t BIT:$Version"
echo "Uptime:$UT"
echo "Last Reboot :$LR"
echo -e "$KR"
echo "---------------------"
((Count++))
done
Count=$((Count-1))
echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
echo "Total number of Servers in file: $Count"
echo "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
 
Old 05-14-2014, 01:49 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I'm not a Bash scripting expert, but it would appear that once you SSH to the remote server, you are expecting the commands that follow (e.g. `rpm -qa | grep kernel`) to execute on that remote server. Well, that's not going to happen.

All you are going to do with your script is connect (via SSH) to the remote server, and then it will sit there until you log out. Then your commands will be execute, but on the local system.

If you want to execute a command on the remote server, try something like:
Code:
KR=$(ssh $server "rpm -qa | grep kernel")
LR=$(ssh $server "last reboot")
...

Last edited by dwhitney67; 05-14-2014 at 01:52 PM.
 
Old 05-14-2014, 01:54 PM   #3
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
Another option would be to use Expect.
 
Old 05-14-2014, 01:56 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
It's not working because you can't use ssh like that. As far as your script is concerned, ssh is one program. Your script won't move to the next line until the ssh program finishes. Which means, your 'KR=`rpm -qa | grep kernel`' line will not run until after you've both logged into AND exited from the server in question and the ssh program exits.

If you want to run those commands on the server through ssh, then you need to put them IN the ssh call.
 
Old 05-14-2014, 02:13 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Something like this would work:

Code:
KR=`ssh -t $Server "rpm -qa | grep kernel"`
LR=`ssh -t $Server "last reboot"`
UT=`ssh -t $Server "uptime"`
OS=`ssh -t $Server "cat /etc/redhat-release"`
Version=`ssh -t $Server "uname -i"`
 
Old 05-14-2014, 03:48 PM   #6
zaibrockstar
LQ Newbie
 
Registered: May 2014
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
Something like this would work:

Code:
KR=`ssh -t $Server "rpm -qa | grep kernel"`
LR=`ssh -t $Server "last reboot"`
UT=`ssh -t $Server "uptime"`
OS=`ssh -t $Server "cat /etc/redhat-release"`
Version=`ssh -t $Server "uname -i"`
This works. Now I get Connection to server closed for every parameter. So I tried to get all parameters in a single session. Can you suggest ?

Code:
OS=`ssh -t $Server "cat /etc/redhat-release" Version="uname -i"`
Code:
OS=`ssh -t $Server "cat /etc/redhat-release";Version="uname -i"`
Code:
OS=`ssh -t $Server "cat /etc/redhat-release;Version="uname -i""`
All This throws error to me
Quote:
bash: -i: command not found
 
Old 05-14-2014, 04:07 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
What do you expect this:
Code:
OS=`ssh -t $Server "cat /etc/redhat-release";Version="uname -i"`
(or either of the other two variants you have) to do?

I think you should read some intro to bash scripting books to learn the basics of bash variables and storing the output of commands. You can't populate two variables from the output of a single command (which is what it appears you're trying to do here).
 
  


Reply



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
shell script ssh to multiple servers hoa Linux - Newbie 1 06-08-2012 12:39 AM
[SOLVED] ssh into multiple servers and tail -f greenpool Linux - Newbie 7 09-28-2011 11:05 PM
LXer: ssh on multiple servers Using cluster ssh LXer Syndicated Linux News 0 01-11-2008 03:40 PM
SSH to multiple servers DigiCrime Linux - General 6 06-22-2006 08:19 AM
Linking to multiple linux servers with SSH rwalkerphl Linux - Newbie 2 07-05-2004 12:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:13 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
Open Source Consulting | Domain Registration