Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

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


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



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"; } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://796816]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2018-01-23 16:20 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (248 votes). Check out past polls.