I am trying get "df -k" output from multiple hosts along with their hostnames via ssh, my script is appending the "df -k" output from all the nodes to a single file but not getting the hostnames for those nodes, just wondering how to pass more than one command via ssh or may be someone could come up with better script.
#!/usr/bin/ksh
# df.ksh
echo "\$\# is $#"
echo "\$1 is $1"
read r
while [ $# -gt 0 ]
do
echo $1
read r
if [ $? = 0 ]
then
ssh root@$1 hostname; df -k >> /tmp/df.out
else
echo "No SSH on root@$1" >> /tmp/nossh.out
fi
done
Does You script really make sense? It looks as if You will always only connect to the hostname in $1 which doesn't change in the loop, and the df -k command will always run on Your local machine.
Anyway, try
ssh root@$1 'hostname; df -k' >> /tmp/df.out
Thanks Lakris, ur right thats not the right one, somehow I pasted it incorrectly, below is the actual one am running and I followed ur suggestion and it seems to be working for me. Thanks again for ur timely response and if have any more suggestions to my script they are more than welcome.
#!/usr/bin/ksh -vx
# df.ksh
echo "\$\# is $#"
echo "\$1 is $1"
read r
while [ $# -gt 0 ]
do
echo $1
read r
ssh root@$1 'hostname; df -k' >> /tmp/df.out
if [ $? = 0 ]
then
ssh root@$1 ssh root@$1 'hostname; df -k' >> /tmp/df.out
if [ $? = 0 ]
then
ssh root@$1
if [ $? = 0 ]
then
ssh root@$1 hostname >> sshsuccess.list
shift
fi
else
echo root@$1 >> sshfailed.list
echo "check .ssh directory"
read nothing
ssh root@$1
fi
else
echo "No SSH on root@$1" >> nossh.list
shift
fi
done
Ok then!
Just a few things...
I edited Your script for readability, removed what i think was redundant "debug"-stuff. See my comments:
#!/usr/bin/ksh -vx
while [ $# -gt 0 ]; do
ssh root@$1 'hostname; df -k' >> /tmp/df.out
if [ $? = 0 ]; then
#If the above is successful, why do You want to do it again?
ssh root@$1 ssh root@$1 'hostname; df -k' >> /tmp/df.out
#Is this really correct? ssh to itself to run the commands?
#Unless You want to make sure it can ssh to itself?
if [ $? = 0 ]; then
#If it was correct the first time, why shouldn't it be the next time?
ssh root@$1
#Do You want to go interactive in a script?
if [ $? = 0 ]; then
ssh root@$1 hostname >> sshsuccess.list
#I guess the presence of output in /tmp/df.out should give enough indication about a hosts accessibility
shift
fi
else
echo root@$1 >> sshfailed.list
echo "check .ssh directory"
ssh root@$1
#If it is inaccessible, do You want to try to connect anyway?
fi
else
echo "No SSH on root@$1" >> nossh.list
shift
fi
done
Thanks again Lakris, for ur valuable suggestions with which I'm able to trim down my script, but there are 2 things that I'm trying to get around with: 1) It is always asking for the passwd even though I copied over the "ssh key" to all the nodes, I have to enter it manually all the time and 2)how to make the script not to try to connect if there is no ssh installed on the system since it is not going to the next node after it encounters the node with no ssh.
1) Have You copied the copied the contents of Your local ~/.ssh/id_rsa.pub and added it to the remote machines /root/.ssh/authorized_keys? Maybe You've overwritten something? It is also necessary the create the public key with EMPTY password/passphrase. Otherwise You will still get a password prompt...
2) Once You get password-less login working, I would guess it would be sufficient with something like, let's say hostlist is a program that generates a list of the hosts You want to connect to: