0

I have file and in this file i have this lines:

ssh root@server1.com
ssh root@server2.com
etc....

And i have script that i like to go on every server and execute the ls command on each of this servers that are on that file. What i did for now is:

while read line;
do
$line
done < $1

and it work but for the first line, after that it return an

Pseudo-terminal will not be allocated because stdin is not a terminal. error.

So i know I'm missing something but I'm a little lost here.

cuonglm
  • 153,898

2 Answers2

2

Add -t option for ssh command in your file:

ssh -t root@server1.com
ssh -t root@server2.com
etc....

From man ssh:

-t      Force pseudo-tty allocation.  This can be used to execute arbi‐
        trary screen-based programs on a remote machine, which can be
        very useful, e.g. when implementing menu services.  Multiple -t
        options force tty allocation, even if ssh has no local tty.

If error still occurs, adding another -t option, something like:

ssh -t -t root@server1.com
cuonglm
  • 153,898
  • Ye i forgot about that, but now it just go to the first server and after that it doesn't continue ... and i add 2 -t ... with the one -t option iv got again the same error. – user3523605 Aug 17 '14 at 19:40
1

You're executing the command ssh root@server1.com, which reads commands from standard input and executes them on server1.com. Standard input is connected to your input file, so on server1.com, you're executing the command ssh root@server2.com.

If you want to execute the command ls, you're going to need to work ls into your code somehow. There are two sane options:

  • If you want to execute arbitrary commands, put the whole commands into your input file:

    ssh root@server1.com ls
    ssh root@server2.com ls
    

    Then your input file is a shell script, just execute it with sh myscript, or make it executable and put #!/bin/sh at the top.

  • If you want to execute the same command on multiple machines, make your input file contain only the machine names, i.e.

    root@server1.com
    root@server2.com
    

    Then read the machine names and execute the command on each:

    while read target comment; do
      ssh "$target" ls </dev/null
    done <"$1"
    

    The redirection from /dev/null is in case the remote command might try to read standard input, to avoid ever reading from the file with the list of machine names. The list file can contain other words on each line after the server name; they'll be stuffed into the variable comment. Get into the habit of putting double quotes around variable substitutions.

    You may be better served by frameworks for executing the same command on multiple machines.