Have you even tried anything I've suggested? They do what you want, even though that changes every other post.
If your script is getting complicated enough, you might want to avoid doing multiple ssh and scp things per server -- that can take a long time -- by combining it all into one script which gets sent to the server. ssh lets you run entire scripts on the remote server easily enough.
ssh username@host exec /bin/sh -s arg1 arg2 < local-file.sh
will act like you've ran local-file.sh on the remote host, with the arguments arg1 arg2
. If you need to pass strings from the local system to the remote one, the arguments are the place to do it. It will print stream data to stdout as usual, which can be redirected in the ways I've already shown you into a local file, or multiple local files, or a local variable.
$ cat script.sh
#!/bin/sh
echo Remote hostname is `hostname`
echo Arguments are $*
exit 0
$ ssh username@server exec /bin/sh -s $HOSTNAME qwertyuipo < script.sh
Remote hostname is steward
Arguments are localname qwertyuipo
$
---------- Post updated at 12:10 PM ---------- Previous update was at 11:49 AM ----------
useless use of cat, and useless use of backticks. We've got to discover who keeps teaching this to people.
You also don't need to reopen the logfile n+1 separate times, you can just redirect the entire loop's output once.
while read host
do
echo "---------------- RUNNING HEALTH CHECK FOR $host ----------------"
ssh $host "cfg2html-linux -xApo /tmp/"
echo "***************** COMPRESSING FILE in $host *********************"
ssh "$host" '/bin/gzip /tmp/"$(hostname)".html'
echo "***************** COPYING FILE TO harp *********************"
ssh $host 'cat /tmp/"$(hostname)".html.gz' > $PATH_TMP/servers/Linux/$host.html.gz
echo "***************** REMOVING FILES from $host *********************"
ssh $host "rm -fr /tmp/`hostname`*"
echo "---------------------------- DONE -----------------------------"
echo
done < $PATH_TMP/servers/host_linux2_test >> $PATH_TMP/Linux_cfg2html.log
And as I suspected, you're doing four ssh/scp things per loop which won't break it, but is going to make the script very slow; and causes all the problems you've been struggling with, like the inability to have a local variable on the remote server. I'd suggest a script like this instead:
while read host
do
# Here-document is an alternative to feeding it a script file
# Everything between <<"EOF" and EOF gets fed into the remote host's shell.
#
# the script creates the html, and compresses it to stdout.
# we take stdout and put it into ${host}-config.gz
# we take stderr and put it into the log file, >&1
ssh $host exec /bin/sh -s $HOSTNAME 2>&1 > ${host}-config.html.gz <<"EOF"
# If you echo anything here, send it into >&2, i.e. stderr
echo "---------------- RUNNING HEALTH CHECK FOR $HOSTNAME ----------------" >&2
echo "---------------- (Remote host is $1) ----------------" >&2
# Everything in this block gets sent literally. $(hostname) is remote.
cfg2html-linux -xApo /tmp/
# gzip to standard output, instead of resaving to file.gz
# standard output goes across the ssh connection and is saved into ${host}-config.html.gz
/bin/gzip - < /tmp/"$(hostname)".html
rm -f /tmp/$(hostname).html
# MUST be at the beginning of the line. Do not indent!
EOF
done < $PATH_TMP/servers/host_linux2_test >> $PATH_TMP/Linux_cfg2html.log
Everything red is run only on the remote host. The green highlights how I'm getting the local HOSTNAME into the remote $1. (HOSTNAME is a variable that gets set for you by your shell.)
---------- Post updated at 12:29 PM ---------- Previous update was at 12:10 PM ----------
Okay I'm really done editing it now I think:wall: