Operating System - HP-UX
1748201 Members
3110 Online
108759 Solutions
New Discussion юеВ

Problem when writing a script to execute commands using passwordless ssh

 
SOLVED
Go to solution
AdRao
Occasional Advisor

Problem when writing a script to execute commands using passwordless ssh

Hi,
I am writing following script to execute commands on multiple remote systems through passwordless ssh.

#!/usr/sbin/ksh
while read remote_host
do
{
echo ${remote_host}
ssh ${remote_host} date
}
done < hostnames.txt
exit

Here hostnames.txt contains all ,y IP's. Problem is script gives the results of first IP of hostnames.txt. Is there any thing which I need to do for my requirement?
11 REPLIES 11
Laurent Menase
Honored Contributor
Solution

Re: Problem when writing a script to execute commands using passwordless ssh

use -n option
or redirect ssh input to ssh -n ${remote_host} date
or
ssh ${remote_host} date
Suraj K Sankari
Honored Contributor

Re: Problem when writing a script to execute commands using passwordless ssh

Hi,
I did like this way

#!/usr/sbin/ksh
for i in `cat hostnames.txt`
do
{
echo $i
ssh $i date
}
done


Suraj
James R. Ferguson
Acclaimed Contributor

Re: Problem when writing a script to execute commands using passwordless ssh

Hi:

> Suraj: I did like this way

The problem with your way is that it doesn't address the use of '-n'. Moreover, instead of using the shell's built-in 'read' (the redirection of input) you spawn an extra process (the 'cat'). This is simply wasteful.

Regards!

...JRF...
Suraj K Sankari
Honored Contributor

Re: Problem when writing a script to execute commands using passwordless ssh

Hi JFR,

Thanks for the information i'll take care next time.

Suraj

Re: Problem when writing a script to execute commands using passwordless ssh

Hello

Actually ssh read its standard input and it interfers whith the read builtin.

First possibility, read your hostname.txt file through another "file descriptor" :

exec 3< hostname.txt
while read -u3 remote_host ; do
print $remote_host
ssh $remote_host date
done
exec 3<&-

Second possibility : try to close the ssh standard input in your loop :

while read remote_host ; do
print $remote_host
ssh $remote_host date 0<&-
done < hostname.txt

This second possibility has the advantage for your script to be able to ran in background (your_script.ksh &) without receiving a SIGTTIN signal from the kernel.

Of course your can mix both read -u3 and ssh ... 0<&-

Cheers,

Jean-Philippe
Dennis Handly
Acclaimed Contributor

Re: Problem when writing a script to execute commands using passwordless ssh

>Jean-Philippe: Actually ssh read its standard input and it interferes with the read builtin.

Yes, so why do all of this hackery when -n (or < /dev/null) solves the problem?

Re: Problem when writing a script to execute commands using passwordless ssh

Dennis,

Sorry, I didn't know the -n option of ssh.

I agree this is more relevant and more elegant than 0<&-.

Cheers,

Jean-Philippe.
Laurent Menase
Honored Contributor

Re: Problem when writing a script to execute commands using passwordless ssh

JP, avoids <&- before starting an other program it can have some unexpected results.

when starting applications are making the assertion that they have
standard in 0
standard out 1
error out 2

For instance the first socket which will be opened will use fd 0 if 0 is closed.
.....
result of closing stdin could have funny and always unexpected results.
So it is always better to use
so to make what you want to do, like I wrote in the first answer the more simple is
to use -n option of ssh, or redirect stdin for ssh to /dev/null.