blib/lib/Net.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 59 CC 5 SCAR 29.7
CC
5
Cov
0%
exec_cmd
30.0
new
2.0
BEGIN
2.0
CRAP 19.5
1 package CGI::AppBuilder::Net;
2  
3 use warnings;
4 use Net::Rexec 'rexec';
5  
6 # require Exporter;
7 @ISA = qw(Exporter CGI::AppBuilder);
8 our @EXPORT = qw();
9 our @EXPORT_OK = qw(exec_cmd
10 );
11 our %EXPORT_TAGS = (
12 all => [@EXPORT_OK],
13 exec => [qw(exec_cmd)],
14 );
15 $CGI::AppBuilder::Message::VERSION = 0.12;
16  
17 =head1 NAME
18  
19 CGI::AppBuilder::Net - Methods used for remote commands and network
20  
21 =head1 SYNOPSIS
22  
23 my $self = bless {}, "main";
24 use CGI::AppBuilder::Net;
25 $self->debug_level(2); # set debug level to 2
26 # The level 3 message will not be displayed
27 $self->echo_msg("This is level 1 message.", 1);
28 $self->echo_msg("This is level 2 message.", 2);
29 $self->echo_msg("This is level 3 message.", 3);
30  
31 =head1 DESCRIPTION
32  
33 The package contains the modules can be used for executing UNIX commands
34 or initiate network connections.
35  
36 =head2 new (ifn => 'file.cfg', opt => 'hvS:')
37  
38 This is a inherited method from CGI::AppBuilder. See the same method
39 in CGI::AppBuilder for more details.
40  
41 =cut
42  
43 sub new {
44 my ($s, %args) = @_;
45 return $s->SUPER::new(%args);
46 }
47  
48 =head2 exec_cmd ($cmd, $pr)
49  
50 Input variables:
51  
52 $cmd - a full unix command with paraemters and arguments
53 $pr - parameter hash ref
54 remote_host - Remote host name or ip address
55 local_host - local host name or ip address
56 remote_usr - Remote user name
57 remote_pwd - Remote user password
58  
59 Variables used or routines called:
60  
61 get_params - get values for multiple parameters
62  
63 How to use:
64  
65 use CGI::AppBuilder::Net qw(:all);
66 # Case 1: hosts are different and without id and password
67 my $cmd = "cat /my/dir/file.txt";
68 my $pr = {datafax_host=>'dfsvr',local_host='svr2'};
69 my @a = $self->exec_cmd($cmd,$pr); # uses rsh to run the cmd
70  
71 # Case 2: different hosts with id and password
72 my $pr = {datafax_host=>'dfsvr',local_host='svr2',
73 datafax_usr=>'fusr', datafax_pwd=>'pwd' };
74 my @a = $self->exec_cmd($cmd,$pr); # uses rexec
75  
76 # Case 3: hosts are the same and just open a file
77 my $pr = {datafax_host=>'dfsvr',local_host='dfsvr'};
78 my $ar = $self->exec_cmd('/my/file.txt',$pr); # case 2:
79  
80 # Case 4: hosts are the same and run a program
81 my $pr = {datafax_host=>'dfsvr',local_host='dfsvr'};
82 my $ar = $self->exec_cmd('cat /my/file.txt',$pr); # case 2:
83  
84  
85 Return: array or array ref
86  
87 This method opens a file or runs a command and return the contents
88 in array or array ref.
89  
90 =cut
91  
92 sub exec_cmd {
93 my $s = shift;
94 my ($cmd, $pr) = @_;
95 my $vs='remote_host,local_host,remote_usr,remote_pwd';
96 my ($dfh,$lsv,$usr,$pwd) = $s->get_params($vs,$pr);
97 $lsv = `hostname` if ! $lsv;
98 my ($rc, @a);
99 if ($dfh ne $lsv) {
100 # croak "ERR: no user name for remote access.\n" if ! $usr;
101 # croak "ERR: no password for user $usr.\n" if ! $pwd;
102 # if ($usr && $pwd) { # use rexec
103 # $s->echo_msg("CMD: $cmd at $dfh for user $usr...");
104 # ($rc, @a) = rexec($dfh, $cmd, $usr, $pwd);
105 # $rc == 0 || $s->echo_msg("WARN: could not run $cmd on $dfh.");
106 # } else { # use rsh
107 my $u = "rsh $dfh $cmd |";
108 my $fh = new IO::File;
109 $fh->open("$u")||$s->echo_msg("WARN: could not run $u: $!.");
110 @a=<$fh>; close($fh);
111 # }
112 } else { # use perl module
113 $s->echo_msg("CMD: $cmd at $lsv...", 1);
114 my $fh = new IO::File;
115 my @b = split /\s+/, $cmd;
116 $cmd .= '|' if $#b > 0;
117 $fh->open("$cmd") || $s->echo_msg("WARN: could not run $cmd: $!.");
118 @a=<$fh>; close($fh);
119 }
120 return wantarray ? @a : \@a;
121 }
122  
123 1;
124  
125  
126 =head1 CODING HISTORY
127  
128 =over 4
129  
130 =item * Version 0.10
131  
132 Extracted exec_cmd from DataFax::StudySubs.
133  
134 =item * Version 0.11
135  
136 No there yet.
137  
138 =back
139  
140 =head1 FUTURE IMPLEMENTATION
141  
142 =over 4
143  
144 =item * no plan yet
145  
146 =back
147  
148 =head1 AUTHOR
149  
150 Copyright (c) 2007 Hanming Tu. All rights reserved.
151  
152 This package is free software and is provided "as is" without express
153 or implied warranty. It may be used, redistributed and/or modified
154 under the terms of the Perl Artistic License (see
155 http://www.perl.com/perl/misc/Artistic.html)
156  
157 =cut
158