|
Revision 3690, 1.7 kB
(checked in by tokuhirom, 5 years ago)
|
r3622@mnk (orig r272): tokuhiro | 2007-02-15 00:29:44 -0800
oops.
|
| Line | |
|---|
| 1 | package Archer::Shell; |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | use Net::SSH; |
|---|
| 5 | use Term::ReadLine; |
|---|
| 6 | use POSIX; |
|---|
| 7 | |
|---|
| 8 | sub new { |
|---|
| 9 | my ( $class, $args ) = @_; |
|---|
| 10 | |
|---|
| 11 | return bless { %$args }, $class; |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | sub run_loop { |
|---|
| 15 | my ($self, ) = @_; |
|---|
| 16 | |
|---|
| 17 | my $term = Term::ReadLine->new('Archer'); |
|---|
| 18 | |
|---|
| 19 | my $HISTFILE = ($ENV{HOME} || ((getpwuid($<))[7])) . "/.archer_shell_history"; |
|---|
| 20 | my $HISTSIZE = 256; |
|---|
| 21 | |
|---|
| 22 | $term->stifle_history($HISTSIZE); |
|---|
| 23 | if (-f $HISTFILE) { |
|---|
| 24 | $term->ReadHistory($HISTFILE) or $self->{context}->log('warn' => "cannot read history file: $!"); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | while ( defined( my $line = $term->readline('archer> ') )) { |
|---|
| 28 | next if $line =~ /^\s*$/; |
|---|
| 29 | $self->catch_run($line); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | print "\n"; |
|---|
| 33 | |
|---|
| 34 | $term->WriteHistory($HISTFILE) or $self->{context}->log('warn' => "perlsh: cannot write history file: $!"); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | sub catch_run { |
|---|
| 38 | my ($self, $cmd) = @_; |
|---|
| 39 | |
|---|
| 40 | my $parallel = $self->{context}->{config}->{global}->{parallel} |
|---|
| 41 | || 'Archer::Parallel::ForkManager'; |
|---|
| 42 | $parallel->use or die $@; |
|---|
| 43 | |
|---|
| 44 | my $manager = $parallel->new; |
|---|
| 45 | $manager->run({ |
|---|
| 46 | elems => $self->{servers}, |
|---|
| 47 | callback => sub { |
|---|
| 48 | my $server = shift; |
|---|
| 49 | $self->callback($server, $cmd); |
|---|
| 50 | }, |
|---|
| 51 | num => $self->{context}->{parallel_num}, |
|---|
| 52 | }); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | sub callback { |
|---|
| 56 | my ( $self, $server, $cmd ) = @_; |
|---|
| 57 | |
|---|
| 58 | Net::SSH::sshopen2( $server, *READER, *WRITER, $cmd ); |
|---|
| 59 | while (<READER>) { |
|---|
| 60 | chomp; |
|---|
| 61 | print "[$server] $_\n"; |
|---|
| 62 | } |
|---|
| 63 | close READER; |
|---|
| 64 | close WRITER; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | 1; |
|---|
| 68 | __END__ |
|---|
| 69 | |
|---|
| 70 | =head1 NAME |
|---|
| 71 | |
|---|
| 72 | Archer::Shell - display shell prompt for remote servers. |
|---|
| 73 | |
|---|
| 74 | =head1 DESCRIPTION |
|---|
| 75 | |
|---|
| 76 | Shell prompt for remote servers. |
|---|
| 77 | |
|---|
| 78 | =head1 FILES |
|---|
| 79 | |
|---|
| 80 | ~/.archer_shell_history |
|---|
| 81 | |
|---|
| 82 | =head1 AUTHORS |
|---|
| 83 | |
|---|
| 84 | Gosuke Miyashita |
|---|
| 85 | Tokuhiro Matsuno |
|---|
| 86 | |
|---|
| 87 | =head1 SEE ALSO |
|---|
| 88 | |
|---|
| 89 | L<Term::ReadLine> |
|---|
| 90 | |
|---|
| 91 | =cut |
|---|