LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-16-2009, 05:00 AM   #1
janak_17
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Rep: Reputation: 0
Gathering data from different servers via Shell Script


Hi,

I need to collect RAM information from 10 diff linux servers.

Server A -- From where the script will run and also store the data in one file
Server1 to 10 -- from where i need RAM info.

SSH automation already made

Can anybody help me writing such script?
 
Old 07-16-2009, 05:16 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Maybe you just need a loop like this
Code:
#!/bin/bash
for ip in {1..10}
do
  ssh user@192.168.1.$ip command
done
 
Old 07-16-2009, 06:35 AM   #3
janak_17
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Seems Script is not coming out automatically.

for hostname in Server1 Server2
do
ssh -p 901 root@$hostname `grep MemTotal /proc/meminfo >>./meminfo.out`
done

I used this script what is happening is

ServerA#./meminfo
grep: can't open /proc/meminfo
Server1# --- I need to put exit manually over here
grep: can't open /proc/meminfo
Server2# --- I need to put exit manually over here
 
Old 07-16-2009, 06:54 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You have to change backticks with double quotes, otherwise the backticks perform command substitution on the local machine and the result is passed as a command to the remote machine. You can do this instead:
Code:
for hostname in Server1 Server2
do
  echo -n "$hostname: " >>./meminfo.out
  ssh -p 901 root@$hostname "grep MemTotal /proc/meminfo" >>./meminfo.out
done
I guess you want to create meminfo.out on the local server, right? I added an echo -n to write the name of the server before the total memory information.
 
Old 07-16-2009, 07:11 AM   #5
janak_17
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Still Same result.

I added a line

#!/bin/bash
for hostname in Server1 Server2
do
echo -n "$hostname: " >>./meminfo.out
ssh -p 901 root@$hostname `grep MemTotal /proc/meminfo >>./meminfo.out`
echo "exit"
done

But its not exiting on its own.
and in the meminfo.out file it displays only

Server1: Server2:
 
Old 07-16-2009, 07:28 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Don't use backticks! Change
Code:
`grep MemTotal /proc/meminfo >> ./meminfo.out`
with
Code:
"grep MemTotal /proc/meminfo" >> ./meminfo.out
The echo exit statement is not needed, since it will not be sent through ssh.
 
Old 07-16-2009, 07:45 AM   #7
janak_17
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Resulting with error

bash: /usr/bin/grep: No such file or directory
bash: /usr/bin/grep: No such file or directory

#!/bin/bash
for hostname in Server1 Server2
do
echo "$hostname: " >>./meminfo.out
ssh -p 901 root@$hostname "/usr/bin/grep MemTotal /proc/meminfo" >>./meminfo.out
done
 
Old 07-16-2009, 08:23 AM   #8
janak_17
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
#!/bin/bash
for hostname in Server1 Server2
do
echo "$hostname: " >>./meminfo.out
ssh -p 901 root@$hostname "`/usr/bin/grep MemTotal /proc/meminfo` >>./meminfo.out"
done

This works fine but gives this error:
grep: can't open /proc/meminfo
grep: can't open /proc/meminfo
----
I tried with this Script with slight modification
#!/bin/bash
for hostname in Server1 Server2
do
echo "$hostname: " >>./meminfo.out
ssh -p 901 root@$hostname "`/usr/bin/free |grep Mem |awk '{print $2}'` >>./meminfo.out"
done

Because normally under /proc its only read permission though it should read but the size is of 0bytes so i changed it to command but then it results into this error

./meminfo1: /usr/bin/free: No such file or directory
./meminfo1: /usr/bin/free: No such file or directory

With only free command it says "Command Not Found."
 
Old 07-16-2009, 10:04 AM   #9
janak_17
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Can anyone help me out please on this.

The script is not giving me the desired result.

meminfo.out file contains only server name but no details of memory.
 
Old 07-16-2009, 01:52 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
ssh -p 901 root@$hostname "`/usr/bin/free |grep Mem |awk '{print $2}'`
I don't understand why you insist with backticks: DON'T PUT BACKTICKS!!! You don't need them. Backticks stats for command substitution, that is the shell substitute the statement inside the backticks with the result of the command, so that you pass to ssh a string which is not a command by itself. Please, try WITHOUT backticks and tell me if you still get the same errors. Thanks.
 
Old 07-16-2009, 01:56 PM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by janak_17 View Post
Resulting with error

bash: /usr/bin/grep: No such file or directory
bash: /usr/bin/grep: No such file or directory

#!/bin/bash
for hostname in Server1 Server2
do
echo "$hostname: " >>./meminfo.out
ssh -p 901 root@$hostname "/usr/bin/grep MemTotal /proc/meminfo" >>./meminfo.out
done
Ok. Here you did not put backticks and the command should be actually executed on the remote server, but the error is self explanatory. Maybe grep is not in /usr/bin on the remote machines. Some systems put it under /bin, anyway it is usually in the PATH, so you can try without the full path. Just "grep" should be enough.

In any case you can try to ssh to one of these machines and see if the grep command works as you expect it to work. Then you can easily put it in a script... without backticks...
 
Old 07-17-2009, 05:01 AM   #12
janak_17
LQ Newbie
 
Registered: Jul 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Cool Man

that worked. the issue was grep its in /bin

Thanks a lot.
 
  


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
What is the easiest way to run a shell script in the background on 10 servers? abefroman Linux - Software 3 03-12-2008 10:24 AM
How to read data from file to use in shell script? ozymandias Linux - Newbie 7 10-27-2006 02:19 PM
Shell Script: Cross-platform backup for distributed servers tedcox Programming 4 09-05-2006 08:36 PM
Need help on shell script for information gathering from router kharerohit Programming 1 03-17-2006 07:37 AM


All times are GMT -5. The time now is 12:59 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