jlhsgcib has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

The following code runs 5 ssh in parallel.

I need a terminal because sudo(8) wants one (using -S doesn't help). Note that I use a key pair to connect, so no password is asked.

#!/usr/bin/perl use strict; use warnings; my $i; for ($i = 0; $i < 5; $i++) { my $pid = fork; if ($pid) { next } exec 'ssh -t 127.0.0.1 "sudo echo hello"'; exit 0; } for (; $i > 0; $i--) { wait }
However, the output is the following:
hello
Connection to 127.0.0.1 closed.
hello
Connection to 127.0.0.1 closed.
hello
     Connection to 127.0.0.1 closed.
hello
     Connection to 127.0.0.1 closed.
hello
     Connection to 127.0.0.1 closed.

And afterward, echo is disabled on the TTY and I need to use "reset".

Do you have any idea about I could solve this, please?

Thank you very much.
Best regards,

-- Jeremie

Replies are listed 'Best First'.
Re: TTY screwed when running multiple ssh
by Illuminatus (Curate) on Sep 12, 2008 at 17:30 UTC
    Any time you start multiple concurrent IO sessions, you are asking for trouble. Why are you using the -t option? Are you really running some curses-like application? If I run your program without the -t option, I don't see the 'connection closed', and the tty is fine. Remember, just because you are using the -t, all of the concurrent output is coming to your shell/tty. It's like having 5 programs opening the same file for writing at the same time. Results are unpredictable.

    Have you considered forking to a perl program using an SSH module? You have more control.

      Hi,

      Thanks for your reply and sorry for not replying earlier I moved my home over the weekend.

      As I said in the initial post, I have to use the -t option because the sudo command requires a TTY. It simply doesn't work if no TTY has been allocated unfortunately.

      Regarding the SSH module, I would like to avoid depending on third-party modules as much as possible, that's what I'm calling ssh directly.

      Regards,
      -- Jeremie

Re: TTY screwed when running multiple ssh
by swampyankee (Parson) on Sep 12, 2008 at 16:40 UTC

    It's not really a Perl question, so expect some flak about that.

    The obvious question is "does echo get disabled on the TTY when you run ssh from the command line?"


    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc