saving output from bash into a file

I am ssh to many servers to get some information... however sometimes the server is unreacheable and i am getting an error. I want to save that output to a file but I am not able to do so...

I want to be able to save output of bash into a file.. so when I run this command on a script

ssh abhapp01i.abh-sort.us.dhl.com

I get this line, saved into a file...

ssh: abhapp01i.abh-sort.us.dhl.com: Temporary failure in name resolution

I do this for may servers and I want to know to which servers I can login or not...

This is my current script...

for host in `cat $PATH_TEMP/servers_test.txt`
        do
                #LINUX SERVERS
#               var=`ssh $host uname -n`
#               echo -n $var ";">> $PATH_TEMP/valeria/reporte_test
                echo -n $(ssh $host cat /usr/openv/netbackup/exclude_list) >> $PATH_TEMP/valeria/reporte_test
                echo >> $PATH_TEMP/valeria/reporte_test
    done

this is the output on my bash when I run the script

[root@harp valeria]# sh unix_servers_test.sh
ssh: abhmq01.abh-sort.us.dhl.com: Temporary failure in name resolution
ssh: abhxdb01d.abh-sort.us.dhl.com: Temporary failure in name resolution
ssh: admpae1-65.phx-dc.dhl.com: Name or service not known

What I want is to have the previos lines added to my file, because the current file I end up looks like this...

/tmp/ /var/tmp/ /var/spool/mqueue /var/spool/sockets /usr/tmp/ /cdrom/ /SD_CDROM/ /mnt/ /tmp_mnt/ /dev/fd/ /proc/ /INFORMIXTMP/ /etc/mnttab core /Contentstore
_AMNO_PROD/ /Contentstore_AMNO_PROD_2/
/tmp/ /var/tmp/ /var/spool/mqueue /var/spool/sockets /usr/tmp/ /cdrom/ /SD_CDROM/ /mnt/ /tmp_mnt/ /dev/fd/ /proc/ /INFORMIXTMP/ /etc/mnttab core

So the first 3 lines that I see on my bash when I run my script need to be added to the file in order to end up with a file like this..

ssh: abhmq01.abh-sort.us.dhl.com: Temporary failure in name resolution
ssh: abhxdb01d.abh-sort.us.dhl.com: Temporary failure in name resolution
ssh: admpae1-65.phx-dc.dhl.com: Name or service not known
/tmp/ /var/tmp/ /var/spool/mqueue /var/spool/sockets /usr/tmp/ /cdrom/ /SD_CDROM/ /mnt/ /tmp_mnt/ /dev/fd/ /proc/ /INFORMIXTMP/ /etc/mnttab core /Contentstore
_AMNO_PROD/ /Contentstore_AMNO_PROD_2/
/tmp/ /var/tmp/ /var/spool/mqueue /var/spool/sockets /usr/tmp/ /cdrom/ /SD_CDROM/ /mnt/ /tmp_mnt/ /dev/fd/ /proc/ /INFORMIXTMP/ /etc/mnttab core

HOW CAN I ACCOMPLISH THIS, THANK YOU!!

for host in `cat $PATH_TEMP/servers_test.txt`
        do
                #LINUX SERVERS
#               var=`ssh $host uname -n`
#               echo -n $var ";">> $PATH_TEMP/valeria/reporte_test
                echo -n $(ssh $host cat /usr/openv/netbackup/exclude_list) >> $PATH_TEMP/valeria/reporte_test
                echo >> $PATH_TEMP/valeria/reporte_test
    done

This has useless use of cat and useless use of backticks all over the place. ( $( ) is roughly equivalent to backticks.) Also, you don't need to tell every separate statement to write to your logfile, you can redirect your entire while loop to get the output of everything inside it.

Also, the errors are going to standard error, fd 2, not the usual standard output. You can redirect them like 2>file, or send them back into standard output with 2>&1

So, how about:

# if you're operating line by line anyway, 'while read' is more efficient than `cat file`
while read host
do
        # since we're running cat anyway, might as well run something that does
        # the entire job instead.  tr here turns newlines into spaces and prints.
        # We quote everything in "" so the single quotes don't get stripped off.
        ssh $host "tr '\n' ' ' < /usr/openv/netbackup/exclude_list" 2>&1
        echo
done < $PATH_TEMP/servers_test.txt > logfile

---------- Post updated at 11:53 AM ---------- Previous update was at 11:45 AM ----------

You don't even need the error text to know whether the server logged in or not, the program tells the shell whether it succeeded or not, directly. Every program you run returns an error code, zero for success, anything else for error.

# if you're operating line by line anyway, 'while read' is more efficient than `cat file`
while read host
do
        # since we're running cat anyway, might as well run something that does
        # the entire job instead.  tr here turns newlines into spaces and prints.
        # We quote everything in "" so the single quotes don't get stripped off.
        # 2>/dev/null gets rid of the error text, if any.
        if ssh $host "tr '\n' ' ' < /usr/openv/netbackup/exclude_list" 2>/dev/null
        then
                echo "$host success" >&2
        else
                echo "$host fail" >&2
        fi

        echo
        # Prints the data outputted by ssh $host into excludedata
        # prints whether it succeeded or failed into logintest
done < $PATH_TEMP/servers_test.txt > excludedata 2> logintest

Thank you...
This accomplish some of what I need, that is printing the output to a file, but not in the way that I want to. I reallly apreciate the help!
this is more of what I need....

abhmq01.abh-sort.us.dhl.com fail
abhxdb01d.abh-sort.us.dhl.com fail
admpae1-65.phx-dc.dhl.com fail
AND WHEN IT SUCCEDS LOGIN IN TO SERVER....TO PRINT THE CONTENTS OF THE FILE /usr/openv/netbackup/exclude_list IN 1 LINE IN THE FORMAT YOU SUGGESTED...("tr '\n' ' ' < /usr/openv/netbackup/exclude_list")

How exactly do you want it, then?

in this example....this is my list of servers

abhmq01
abhxdb01
admpae1
admpce02
admtdb2

this is my script...

while read host
do
        # since we're running cat anyway, might as well run something that does
        # the entire job instead.  tr here turns newlines into spaces and prints
.
        # We quote everything in "" so the single quotes don't get stripped off.
        # 2>/dev/null gets rid of the error text, if any.
        if ssh $host "tr '\n' ' ' < /usr/openv/netbackup/exclude_list" 2>/dev/nu
ll
        then
                echo $(cat /usr/openv/netbackup/exclude_list) >&2
        else
                echo "$host fail" >&2
        fi

        echo
        # Prints the data outputted by ssh $host into excludedata
        # prints whether it succeeded or failed into logintest
done < servers_test > excludedata 2> logintest

However... I am not getting any input from server #4. This is my output.

abhmq01.abh-sort.us.dhl.com fail
abhxdb01d.abh-sort.us.dhl.com fail
admpae1-65.phx-dc.dhl.com fail
---> (I am missing input from server #4) this line was added by me to explain
/tmp/ /var/tmp/ /var/spool/mqueue /var/spool/sockets /usr/tmp/ /cdrom/ /SD_CDROM/ /mnt/ /tmp_mnt/ /dev/fd/ /proc/ /INFORMIXTMP/ /etc/mnttab core

However, the content of the file is written to the excludedata!

It goes into excludedata because that's where you tell it to go.

Imagine you're putting ssh host cat file into a script file, and running the script. ./script.sh[/icode would print it to your terminal. ./script.sh > excludedata would go into excludedata.

The idea of redirecting the end of a while-loop is the same. We can capture the output of that whole section of code, just like it was a separate script file. It's one of the shell's handier features.

while read host
do
        # think of 'tr' like 'cat' except here it changes newlines into spaces.
        # You don't have to mangle the text in your shell, it's already been
        # catted into standard output.  Which goes into excludedata.
        if ssh $host "tr '\n' ' ' < /usr/openv/netbackup/exclude_list"
        then
                # This won't work because it runs locally.
                # It's ALREADY BEEN PRINTED anyway.  
#              echo $(cat /usr/openv/netbackup/exclude_list) >&2

                echo # add a newline to the end, because tr deleted them all
        else
                # If it fails, it won't print any data, so we print something else.
                echo "$host fail"
        fi

done < servers_test > excludedata 2> logintest

excludedata will contain the contents of each server's /usr/openv/netbackup/exclude_list, or "hostname fail" if it couldn't get through to it. logintest will contain the error output of ssh such as 'ssh: abhmq01.abh-sort.us.dhl.com: Temporary failure in name resolution' if anything couldn't be contacted. This way you can keep the data and error messages separate to make your output much easier to use later.