1package Net::Rsh; 2 3use strict; 4use IO::Socket; 5use Carp; 6use Errno; 7 8require Exporter; 9 10use vars qw($VERSION @ISA @EXPORT); 11 12$VERSION=0.05; 13 14@ISA = qw(Exporter); 15@EXPORT = qw(&rsh); 16 17sub new { 18 my $class=shift; 19 my $self={ }; 20 return bless $self,$class; 21} 22 23sub rsh { 24 my ($self,$host,$local_user,$remote_user,$cmd)=@_; 25 croak("Usage: \$c->rsh(\$host,\$local_user,\$remote_user,\$cmd)") unless @_ == 5; 26 27 my $start_port=512; 28 my $end_port=1023; 29 30 my $try=1; 31 my $port=$end_port; 32 my $socket; 33 34 while($try) { 35 if($port<$start_port) {croak "All ports in use";} 36 $socket = IO::Socket::INET->new(PeerAddr=>$host, 37 PeerPort=>'514', 38 LocalPort=>$port, 39 Proto=>"tcp"); 40 41 if(!defined $socket) { 42 if ($!{EADDRINUSE} || $!{ECONNREFUSED}) { 43 $port-=1; 44 } else { 45 croak("$!"); 46 } 47 } else { $try=0; } 48 } 49 50 print $socket "0\0"; 51 print $socket "$local_user\0"; 52 print $socket "$remote_user\0"; 53 print $socket "$cmd\0"; 54 my @return=<$socket>; 55 return @return; 56} 57 58END { } 59 601; 61 62__END__ 63 64=head1 NAME 65 66Net::Rsh - perl client for Rsh protocol 67 68=head1 SYNOPSIS 69 70 use Net::Rsh; 71 72 $a=Net::Rsh->new(); 73 74 $host="cisco.router.com"; 75 $local_user="root"; 76 $remote_user="root"; 77 $cmd="sh ru"; 78 79 @c=$a->rsh($host,$local_user,$remote_user,$cmd); 80 81 print @c; 82 83 84=head1 DESCRIPTION 85 86 Rsh protocol requires that the program be 87 run as root or that the program be setuid to root 88 89=head1 AUTHOR 90 91Oleg Prokopyev, <riiki@gu.net> 92 93=cut 94