Hi,
I'm getting error while getting output of home directories for multiple servers. Could anyone please help; (using HP-UX; shell=ksh)
SERVERLIST=serverlist.txt
OUTPUTLIST=output.txt
for host in $(cat ${SERVERLIST})
do
ssh $(host) "`hostname`;`echo $HOME`" > ${OUTPUTLIST}
done
Error:
ssh: Server1;/path/dir: host nor service provided, or not known
Usage: host [-aCdlrTwv] [-c class] [-n] [-N ndots] [-t type] [-W time]
[-R number] hostname [server]
There are quite some errors and opportunitites in your code:
the $(host) is a "command substitution", not a variable expansion. Use braces instead (as you do for the list files).
the �hostname� is a "command substitution" again, making the remote shell try to execute the result of the hostname command, not the command itself. Remove backticks.
same for the echo command.
UUOC (useless use of cat) in the for loop. Better: redirected while read .
for more than one server in the list, all results except for the very last one will be overwritten in the output file. Use append redirection ( >> ) or shift the redirection.
Consider
while read host
do ssh $host "hostname; echo $HOME"
done < ${SERVERLIST} > ${OUTPUTLIST}
That goes into the next trap: ssh reads from stdin i.e. the while loop's input.
A fix is -n .
Further, it makes more sense to print the remote $HOME; within 'ticks' the local shell does not substitute it, and the remote shell gets the $HOME without the ticks.
while read host
do
ssh -n $host 'hostname; echo $HOME'
done < ${SERVERLIST} > ${OUTPUTLIST}
Thank you vbe, MadeInGermany for your responses.
vbe: yes i'll keep that in mind not to use keywords.
MadeInGermany: i did
type ssh
and got
ssh is /usr/bin/ssh
. I ran the script without using the one suggested by you. What i'm seeing is 4 banners of HP UX and then the error message as i've posted above. So looks like it's logging into all the 4 servers mentioned in the
serverlist.txt
but unable to get command output. However i'll try with your suggested script. But i need to understand it.
---------- Post updated at 08:08 AM ---------- Previous update was at 07:59 AM ----------
Hi MadeInGermany: just tried with your suggested script. I'm getting 4 HP-UX banners (because i've mentioned only 4 servers in the server list) but not seeing the error as mentioned above. Also not seeing the hostname and home directory in the output.