I'm trying to write a little utility that'll connect to a number of boxes on my network, collect some system information, store them in local text files, then update a database with that information.
At the moment, I'm trying to work out the ssh issues.
It was recommended to me to use here docs to pass a lot of commands to the ssh command for remote execution.
Unfortunately, when using that it seems to execute the commands before the connection is established, as all the commands fail.
Here's my code:
Code:
while read linevar
do
ssh -n cunnings@${linevar} hostname <<END_OF_SSH
echo "Connected to $linevar!"
### Obtain system information ###
node=$(uname -n | tr '[A-Z]' '[a-z]')
os=$(uname)
if test $os = AIX ; then
osv=`oslevel`
ip=$(ping -a -c1 $linevar | head -1 | awk '{print $3}' | tr -d ":")
mem=$(lsattr -E -l mem0 | tail -1 | awk '{print $2}')
cpus=$(lscfg | grep proc | wc -l | awk '{print $1}')
fi
if test $os = SunOS ; then
osv=`uname -r`
ip=$(ping -a -c1 dingo | head -1 | awk '{print $2}')
mem=$(/usr/sbin/prtconf | grep 'Memory size' | awk '{print $3}')
cpus=$(/usr/sbin/psrinfo | wc -l | awk '{print $1}')
fi
if test $os = Linux ; then
osv=`uname -r`
ip=$(ping -c1 $linevar | head -1 | awk '{print $3}' | tr -d ":")
mem=$(grep MemTotal /proc/meminfo | tail -1 | awk '{print $2}')
cpus=$(cat /proc/cpuinfo | grep ^processor| wc -l | awk '{print $1}')
let "mem = $mem / 1024"
fi
if test -r ../data/node_details.txt ; then
rm ../data/node_details.txt
else
touch ../data/node_details.txt
fi
echo "Node name : $node" >> node_details.txt
echo "OS Name : $os" >> node_details.txt
echo "OS Version : $osv" >> node_details.txt
echo "IP Address : $ip" >> node_details.txt
echo "Memory : $mem MB" >> node_details.txt
echo "CPU Count : $cpus" >> node_details.txt
END_OF_SSH
done < node_list.txt
And here's the output (note: username is just a substitution for my own username):
Code:
/home/username/usc/scripts/usc_main: oslevel: command not found
ping: invalid option -- a
Usage: ping [-LRUbdfnqrvV] [-c count] [-i interval] [-w wait]
[-p pattern] [-s packetsize] [-t ttl] [-I interface address]
[ -T timestamp option ] [ -Q tos ] host
lsattr: invalid option -- E
Usage: lsattr [-RVadlv] [files...]
/home/username/usc/scripts/usc_main: lscfg: command not found
ping: invalid option -- a
Usage: ping [-LRUbdfnqrvV] [-c count] [-i interval] [-w wait]
[-p pattern] [-s packetsize] [-t ttl] [-I interface address]
[ -T timestamp option ] [ -Q tos ] host
/home/username/usc/scripts/usc_main: /usr/sbin/prtconf: No such file or directory
/home/username/usc/scripts/usc_main: /usr/sbin/psrinfo: No such file or directory
username@lightning2's password:
lightning2
/home/username/usc/scripts/usc_main: oslevel: command not found
ping: invalid option -- a
Usage: ping [-LRUbdfnqrvV] [-c count] [-i interval] [-w wait]
[-p pattern] [-s packetsize] [-t ttl] [-I interface address]
[ -T timestamp option ] [ -Q tos ] host
lsattr: invalid option -- E
Usage: lsattr [-RVadlv] [files...]
/home/username/usc/scripts/usc_main: lscfg: command not found
ping: invalid option -- a
Usage: ping [-LRUbdfnqrvV] [-c count] [-i interval] [-w wait]
[-p pattern] [-s packetsize] [-t ttl] [-I interface address]
[ -T timestamp option ] [ -Q tos ] host
/home/username/usc/scripts/usc_main: /usr/sbin/prtconf: No such file or directory
/home/username/usc/scripts/usc_main: /usr/sbin/psrinfo: No such file or directory
username@ranger's password:
ranger
The text file being read in contains 2 lines:
LIGHTNING2
RANGER
I'm running the script on RH, under the korn and/or bash.