Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: IO::Select & ssh problem

by cmv (Chaplain)
on Sep 22, 2009 at 20:18 UTC ( #796816=note: print w/ replies, xml ) Need Help??


in reply to IO::Select & ssh problem

Folks-

After lots of twiddling, and some CB help (thanks ssandv++ clinton++), here is the version that seems to work best. Still have some strange things going on (like stdout seems to be in raw mode, and sometimes I control-c doesn't work), but overall it seems to be doing what I expect.

Use this when you need to run tail -f on a file that exists on a bunch of hosts, and want the output interleaved correctly.

Please make comments and/or give me alternative ways of doing this (under perl5.6 with no other modules but IO::Select).

Thanks

-Craig

use strict; use warnings; use IO::Select; $|++; my $pkey = "$ENV{HOME}/.ssh/PrivateKey"; my @logins = ( qw ( usr@host1 usr@host2 usr@host3) ); my $cmd = "'tail -f /tmp/growingfile'"; my @shell = ( '/usr/bin/ksh' => ( -c => "$cmd" )); my @FDs; # Create ssh tails as FDs on child processes... foreach my $login (@logins) { print STDERR "login=$login\n"; my @sshcmd = ('/usr/bin/ssh' => ( -t => (), #-v => (), -i => $pkey, -o => 'StrictHostKeyChecking no', $login, @shell, )); my $fd; if(my $child = open ($fd, '-|')) { print STDERR "I'm the parent\n"; print STDERR "PARENT: \$!: $!...\n"; print STDERR "Child pid=$child\n"; }else{ $|++; print STDERR "I am the child\n"; exec @sshcmd; } push(@FDs, \*$fd); } my $sel = new IO::Select(@FDs); while(my @ready = $sel->can_read) { foreach my $fh (@ready) { if(eof($fh)) { $sel->remove($fh); close($fh); next; } my $line = <$fh>; print "$fh: $line"; } }


Comment on Re: IO::Select & ssh problem
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://796816]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2015-07-06 07:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls