Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Running RHEL 6.2, trying to write a bash script to SSH to a list of remote servers, and write the CPUs and Total Memory to a file, one line per host in the following format:

HOSTNAME1    CPUS: 12    MEMORY: 64
HOSTNAME2    CPUS: 08    MEMORY: 12

Here is what I have so far, it doesn't work fully, what I'm running in to is the part where the system SSHs to run cat /proc/cpuinfo and free.

The bash script would be called like so: ./query_host_info.sh <DEST> <USER> <FILE>

The file it reads the lists of hosts from is a one-per-line file of hostnames.

#!/bin/bash

# username to connect via ssh
USER=$2
# destination path/filename to save results to
DEST=$3
# source list of hostnames to read from
FILE=$1

# Iterate through line items in FILE and
# execute ssh, if we connected successfully
# run proc/info and free to find memory/cpu alloc
# write it to DEST path/file
# if we dont connect successfully, write the hostname
# and "unable to connect to host" error to DEST path/file
for i in `cat $FILE`; do
  echo -n ".";
  CHK=`ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" \
            -l $USER $i "echo success"`;
  if [ "success" = $CHK ] >/dev/null 2>&1
  then
    `ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $i "\
        printf "$i    ";
        echo "`cat /proc/cpuinfo | grep processor | awk '{a++} END {print a}';
        free -g | sed -n -e '/^Mem:/s/^[^0-9]*\([0-9]*\) .*/\1/p'`";" >> ${DEST}`;
  else
    printf "${i}\tUnable to connect to host\n" >> ${DEST};
  fi
done
# All line items have been gone through,
# show done, and exit out
echo ""
echo "Done!"
echo "Check the list 'checkssh_failure' for errors."
exit 0
share|improve this question
    
You want to collect the results from the script on the host running the script, right? –  slm Feb 6 '13 at 21:42
    
this bash script will be stored on my machine locally, It will then iterate through a list of hostnames ($i), connect to each hostname, query that hostname for its memory/cpu allocated. and then write that data to a file on my machine ($DEST), one per line on the $DEST file. –  gorelative Feb 6 '13 at 21:46
    
I think its related to nested quotes and i think im just getting a few syntax related issues.. –  gorelative Feb 6 '13 at 21:47

3 Answers 3

Can't you just run the script like so?:

% ./query_host_info.sh <DEST> <USER> <FILE> > output.log 

-or-

% ./query_host_info.sh <DEST> <USER> <FILE> | tee output.log

Additionally you can use a command list, wrap a bunch of commands in parens, and redirect their output to a file.

% (ls; ls; ls;) > some_output.log
share|improve this answer
    
I could absolutely do that, my problem though is getting the ` ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $i "\ printf "$i "; echo "cat /proc/cpuinfo | grep processor | awk '{a++} END {print a}'; free -g | sed -n -e '/^Mem:/s/^[^0-9]*([0-9]*) .*/\1/p'";" >> ${DEST};` to work properly. –  gorelative Feb 6 '13 at 21:58
    
Yeah I just noticed it wasn't working, let me take a look, it's definitely a escaping issue, but what you're doing is doable. –  slm Feb 6 '13 at 22:01
    
Figured it out one way, posted my answer. –  gorelative Feb 6 '13 at 22:25

I figured it out.. two things.. SSH didnt like the concatenated proc/cpuinfo and free, and i reorganized how the commands are ran.. Probably a way better way of doing this but this seems to work..

#!/bin/bash

# username to connect via ssh
USER=$2
# destination path/filename to save results to
DEST=$3
# source list of hostnames to read from
FILE=$1

# Iterate through line items in FILE and
# execute ssh, if we connected successfully
# run proc/info and free to find memory/cpu alloc
# write it to DEST path/file
# if we dont connect successfully, write the hostname
# and "unable to connect to host" error to DEST path/file
for i in `cat $FILE`; do
  echo -n ".";
  CHK=`ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" \
            -l $USER $i "echo success"`;
  if [ "success" = $CHK ] >/dev/null 2>&1
  then
    A=`ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $i "cat /proc/cpuinfo | grep processor | awk '{a++} END {print a}'"`
    B=`ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $i "free -g | sed -n -e '/^Mem:/s/^[^0-9]*\([0-9]*\) .*/\1/p'"`
    echo "${i} CPU: ${A} MEMORY: ${B}" >> ${DEST}
  else
    printf "${i}\tUnable to connect to host\n" >> ${DEST};
  fi
done
# All line items have been gone through,
# show done, and exit out
echo ""
echo "Done!"
echo "Check the list 'checkssh_failure' for errors."
exit 0
share|improve this answer

Just modified your script :

#!/bin/bash

# username to connect via ssh
USER=$2
# destination path/filename to save results to
DEST=$3
# source list of hostnames to read from
FILE=$1

[[ $# -ne 3 ]] && { echo -e "\nUsage: $0  <User> <ServerList> <LogFile>\n"; exit 1; };

func_ssh() {

    local Ipaddr=$1
    local Cmd="${@:2}"
    local LogIt=${DEST}
    ssh -q -o "BatchMode yes" -o "ConnectTimeout 5" -l $USER $Ipaddr "${Cmd}"
    [[ $? -ne 0 ]] && printf "${Ipaddr}\tUnable to connect to host\n" >> ${LogIt}
}

GetTotalProcs="awk '/processor/{a++} END{print a}'  /proc/cpuinfo"
GetMemoryDetails="free -g | sed -n -e '/^Mem:/s/^[^0-9]*\([0-9]*\) .*/\1/p'"

# Iterate through line items in FILE and
# execute ssh, if we connected successfully
# run proc/info and free to find memory/cpu alloc
# write it to DEST path/file
# if we dont connect successfully, write the hostname
# and "unable to connect to host" error to DEST path/file
for srv in $(< $FILE );
do
    echo -n "."
    A="$( func_ssh $srv $GetTotalProcs )"
    B="$( func_ssh $srv $GetMemoryDetails )"
    echo "${srv} CPU: ${A} MEMORY: ${B}" >> ${DEST}

done

# All line items have been gone through,
# show done, and exit out
echo ""
echo "Done!"
echo "Check the list 'checkssh_failure' for errors."
exit 0
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.