Error with script for ssh

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}
1 Like

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}
1 Like

Thank you very much RudiC and MadeInGermany for your inputs. I'm getting below error when i used while loop as mentioned above;

Pseudo-terminal will not be allocated because stdin is not a terminal.
ssh: hostname;echo /home/dir: host nor service provided, or not known

shell script i used:

SERVERLIST=serverlist.txt
OUTPUTLIST=output.txt
while read host
do
ssh -n $host "hostname;echo $HOME"
done < ${SERVERLIST} >> ${OUTPUTLIST}

What's the value of the "RequestTTY" option in ssh_config , if any?

while read host
do
ssh -n $host "hostname;echo $HOME"

You should NEVER name a variable the same as a reserved word : host ( usually system variable, or like here a true UNIX command...)

1 Like

The use of $host is no problem.
Do you have ssh aliased or another ssh in your PATH?

type ssh

should find out.
The following filters some rubbish from the input file

PATH=/bin:/usr/bin:/sbin:/usr/sbin
SERVERLIST=serverlist.txt
OUTPUTLIST=output.txt
while read host junk
do
  case $host in
  [!#]*)
    ssh -n "$host" 'hostname; echo $HOME'
  ;;
  esac
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. :slight_smile:

---------- 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.

The output is redirected to $OUTPUTLIST i.e. the file output.txt

cat output.txt
1 Like

MadeInGermany: Thanks. I'm getting output in output.txt. I totally forgot about it. In the code i included

done < ${SERVERLIST} > ${OUTPUTLIST} 2> /dev/null

which's suppressing the HP UX banners & the error mesg.
Thanks once again.