Executing remote commands via ssh

Hi,
I'm tryin to write a script that will collect information about a remote servers, put them into variables and print them to screen.

# /usr/bin/bash
ls $1 > /dev/null 2>/dev/null
if [ "$?" -eq 0 ]
then
  echo "$1 is file"
        for server in $(cat $1)
        do
#       echo $server
                ssh -T $server /bin/bash -s <<EOF
                HOST1=$(hostname)     #1
                `hostname`       #2
EOF
echo $HOST1
        done
else
 echo "$1 is not file"

#ssh $1
fi

the output of the 1st variable is nothing,
and the output of the command is the name of the local machine.

thanks a lot.

That seems somewhat convoluted.

if [ -f "$1" ]; then
  while read SERVER; do
    HOST1=$(ssh -T $SERVER "\$(hostname)")
    echo $HOST1
  done < "$1"
else
  echo "File not found"
fi

OK thanks, it's a kind of solution. but, if I want to take a lot of information and put that in variables (like: mem_usage, CPU, and more) i don't want to do more than one SSH connection per server.
I prefer to do that with one SSH connection and EOF.

tnx again.

Use a here-document like <<"EOF", which will not evaluate the contents locally.

1 Like

I did not understand..
Can you explain?

You could always try it and see...

$ cat <<EOF
`echo asdf`
EOF

asdf

$ cat <<"EOF"
`echo asdf`
EOF

`echo asdf`

$