script for df output from multiple hosts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script for df output from multiple hosts
# 1  
Old 11-26-2007
script for df output from multiple hosts

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

Thanks,
Barkath
# 2  
Old 11-27-2007
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
# 3  
Old 11-27-2007
script for df output from multiple hosts

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
# 4  
Old 11-27-2007
Ok then! Smilie
Just a few things...
I edited Your script for readability, removed what i think was redundant "debug"-stuff. See my comments:

Code:
#!/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

Just a few thought, since You "asked" for it.

/Lakris
# 5  
Old 11-27-2007
script for df output from multiple hosts

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.
# 6  
Old 11-28-2007
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:
Quote:
for x in `hostlist`; do
ssh root@$x 'hostname; df -k' >> /tmp/df.out
[[ ! $? = 0 ]] && echo Failure, errno $?, can not connect to host $x >>sshfailed.list
done
well, just a prototype anyway.
# 7  
Old 11-29-2007
script for df output from multiple hosts

Larkis,
1)Yes I did copied over the ssh key to all the nodes using the below script which is similar to the one above,

#!/usr/bin/ksh

# install public key on all aix systems for RSA-auth root access and test

while [ $# -gt 0 ]
do
echo $1
read r
scp /.ssh/id_rsa.pub ${1}:/tmp/tsm.pub
if [ $? = 0 ]
then
ssh root@$1 "cat /tmp/tsm.pub >> ~/.ssh/authorized_keys"
if [ $? = 0 ]
then
ssh root@$1
if [ $? = 0 ]
then
ssh root@$1 hostname >> sshpass
shift
fi
else
echo root@$1 >> sshfail
echo "check .ssh directory"
ssh root@$1
fi
else
echo "No SSH on root@$1" >> nossh
shift
fi
done

2)u really made the script look short and simple, Great!

Thanks much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk remote multiple hosts print remote hostname and output

Hi all, i'm trying to gether multiple pattern on remote hosts, and trying to print hostname and the pattern, ssh remoteserver1 -C 'hostname 2>&1;cat /var/log/server1.log | awk -F ";" '"'"'{ print " "$2" "$5}'"'"'| sort | uniq -c | sort -g -r ' The output is the following, remoteserver1 ... (8 Replies)
Discussion started by: charli1
8 Replies

2. Shell Programming and Scripting

Grep remote multiple hosts output to local server

Hello all, i'm trying to create a report by greping a pattern on multiple remote hosts and creta a simple report, actually i did this, is ther any better way to do this. #!/bin/bash for host in `cat RemoteHosts` do ssh $host -C 'hostname 2>&1; grep ERROR /var/log/WebServer.log.2019-09-21... (0 Replies)
Discussion started by: charli1
0 Replies

3. Shell Programming and Scripting

Ssh to multiple hosts and then run multiple for loops under remote session

Hello, I am trying to login to multiple servers and i have to run multiple loops to gather some details..Could you please help me out. I am specifically facing issues while running for loops. I have to run multiple for loops in else condition. but the below code is giving errors in for... (2 Replies)
Discussion started by: mohit_vardhani
2 Replies

4. Shell Programming and Scripting

Check connectivity with multiple hosts - BASH script available here

Hi everyone! Some time ago, I had to check connectivity with a big list of hosts, using different formats (protocol://server:port/path/, server:port, ....). I developed a script that checks the connectivity using different commands (ping, telnet, nc, curl). It worked for me so I'm sharing it... (9 Replies)
Discussion started by: Fr3dY
9 Replies

5. Shell Programming and Scripting

How to write bash script for creating user on multiple Linux hosts?

I wonder whether someone can help me with what I'm trying to achieve Basically, the objective is one script to create new user on more than 70 linux hosts if required. Everything works apart from the highlighted part. It gave me an output passwd: Unknown user name ''. when try to set... (35 Replies)
Discussion started by: fugeulu
35 Replies

6. Shell Programming and Scripting

Running a script on multiple remote hosts at once

I have a script on about 15 hosts that I need to run for each host whenever I want (not crontab). Problem is, this script takes 5-10 mins to run for each host. Is there a way I can run the script in parallel for all the hosts instead of 1 at a time? Also, I'm remotely running the script on the... (3 Replies)
Discussion started by: mrskittles99
3 Replies

7. UNIX for Advanced & Expert Users

remote hosts and wide output?

Hello forum: I am curious about some output that I get using an alias <command> on a remote host and I wondered if someone could point me in the right direction. Symptoms: Using "ssh -qi /path/to/key root@som.ipa.ddr.ess mail" (or variation of via alias) only gives a partial textual... (7 Replies)
Discussion started by: Habitual
7 Replies

8. Shell Programming and Scripting

script to reboot multiple hosts

Hi Expert, How to create a script to reboot multiple hosts in linux? Thank you. (5 Replies)
Discussion started by: regmaster
5 Replies

9. Shell Programming and Scripting

ssh to multiple hosts and saving the output in the local host

hi I have a script to login from a host "A" to a list of hosts in a file and perform some commands inside it...its somethin like this for i in `cat file` do ssh -t $i " command1 ; command2; ..." done I wanna save the outputs in a file in the current host "A" i.e from where I am... (3 Replies)
Discussion started by: ningy
3 Replies

10. UNIX for Advanced & Expert Users

Copy a file to multiple hosts

Hi, I need to copy a file ( say 1MB file ) to multiple hosts( no of machines is huge). What would be the most optimal way of doing it with minimal user intervention ? Thanks, Sumit (5 Replies)
Discussion started by: sumsriva
5 Replies
Login or Register to Ask a Question