################################################################## # Net::SCP::Expect # # Wrapper for scp, with the ability to send passwords via Expect. # # See POD for more details. ################################################################## package Net::SCP::Expect; use strict; use Expect; use POSIX qw(:signal_h WNOHANG); use File::Basename; use Carp; use Cwd; $SIG{CHLD} = \&reapChild; BEGIN{ use vars qw/$VERSION/; $VERSION = '.12'; } # Options added as needed sub new{ my($class,%arg) = @_; my $self = { _host => $arg{host}, _user => $arg{user} || $ENV{'USER'}, _password => $arg{password}, _cipher => $arg{cipher}, _port => $arg{port}, _error_handler => $arg{error_handler}, _preserve => $arg{preserve} || 0, _recursive => $arg{recursive} || 0, _verbose => $arg{verbose} || 0, _auto_yes => $arg{auto_yes} || 0, _terminator => $arg{terminator} || "\n", _timeout => $arg{timeout} || 10, _timeout_auto => $arg{timeout_auto} || 1, _timeout_err => $arg{timeout_err} || undef, _no_check => $arg{no_check} || 0, _protocol => $arg{protocol} || undef, _identity_file => $arg{identity_file} || undef, _option => $arg{option} || undef, _subsystem => $arg{subsystem} || undef, }; bless($self,$class); } sub _get{ my($self,$attr) = @_; return $self->{"_$attr"}; } sub _set{ my($self,$attr,$val) = @_; croak("No attribute supplied to 'set()' method") unless defined $attr; $self->{"_$attr"} = $val; } sub auto_yes{ my($self,$val) = @_; croak("No value passed to 'auto_yes()' method") unless defined $val; $self->_set('auto_yes',$val); } sub error_handler{ my($self,$sub) = @_; croak("No sub supplied to 'error_handler()' method") unless defined $sub; $self->_set('error_handler',$sub) } sub login{ my($self,$user,$password) = @_; croak("No user supplied to 'login()' method") unless defined $user; croak("No password supplied to 'password()' method") unless defined $password; $self->_set('user',$user); $self->_set('password',$password); } sub password{ my($self,$password) = @_; croak("No password supplied to 'password()' method") unless $password; $self->_set('password',$password); } sub host{ my($self,$host) = @_; croak("No host supplied to 'host()' method") unless $host; $self->_set('host',$host); } sub user{ my($self,$user) = @_; croak("No user supplied to 'user()' method") unless $user; $self->_set('user',$user); } #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # If the hostname is not included as part of the source, it is assumed to # be part of the destination. #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ sub scp{ my($self,$from,$to) = @_; my $login = $self->_get('user'); my $password = $self->_get('password'); my $timeout = $self->_get('timeout'); my $timeout_auto = $self->_get('timeout_auto'); my $timeout_err = $self->_get('timeout_err'); my $cipher = $self->_get('cipher'); my $port = $self->_get('port'); my $recursive = $self->_get('recursive'); my $verbose = $self->_get('verbose'); my $preserve = $self->_get('preserve'); my $handler = $self->_get('error_handler'); my $auto_yes = $self->_get('auto_yes'); my $no_check = $self->_get('no_check'); my $terminator = $self->_get('terminator'); my $protocol = $self->_get('protocol'); my $identity_file = $self->_get('identity_file'); my $option = $self->_get('option'); my $subsystem = $self->_get('subsystem'); ################################################################## # If the second argument is not provided, the remote file will be # given the same (base) name as the local file (or vice-versa). ################################################################## unless($to){ $to = basename($from); } my($host,$dest); # Parse the to/from string. If the $from contains a ':', assume it is the remote host if($from =~ /:/){ ($login,$host,$dest) = $self->_parse_scp_string($from); $from = "$login\@$host:"; $from .= "$dest" if $dest; } else{ ($login,$host,$dest) = $self->_parse_scp_string($to); $to = "$login\@$host:"; $to .= "$dest" if $dest; } croak("No login. Can't scp") unless $login; croak("No password. Can't scp") unless $password; croak("No host specified. Can't scp") unless $host; # Gather flags. my $flags; $flags .= "-c $cipher " if $cipher; $flags .= "-P $port " if $port; $flags .= "-r " if $recursive; $flags .= "-v " if $verbose; $flags .= "-p " if $preserve; $flags .= "-$protocol " if $protocol; $flags .= "-q "; # Always pass this option (no progress meter) $flags .= "-s $subsystem" if $subsystem; $flags .= "-o $option" if $option; $flags .= "-i $identity_file" if $identity_file; my $scp = Expect->new; #if($verbose){ $scp->raw_pty(1) } #$scp->debug(1); my $scp_string = "scp $flags $from $to"; $scp = Expect->spawn($scp_string) or croak "Couldn't start program: $!\n"; $scp->log_stdout(0); if($auto_yes){ while($scp->expect($timeout_auto,-re=>'[Yy]es\/[Nn]o')){ $scp->send("yes\n"); } } unless($scp->expect($timeout,-re=>'[Pp]assword.*?:|[Pp]assphrase.*?:')){ my $err = $scp->before() || $scp->match(); if($err){ if($handler){ $handler->($err) } croak("Problem performing scp: $err"); } croak("scp timed out while trying to connect to $host"); } if($verbose){ print $scp->before() } $password .= $terminator if $terminator; $scp->send($password); ################################################################ # Check to see if we sent the correct password, or if we got # some other bizarre error. Anything passed back to the # terminal at this point means that something went wrong. # # The exception to this is verbose output, which can mistakenly # be picked up by Expect. ################################################################ unless($no_check || $verbose){ $scp->expect($timeout_err, [qr/[Pp]ass.*/ => sub{ my $error = $scp->before() || $scp->match(); if($handler){ $handler->($error); } else{ croak("Error: Bad password [$error]"); } } ], [qr/\w+.*/ => sub{ my $error = $scp->match() || $scp->before(); if($handler){ $handler->($error); } else{ croak("Error - last line returned was: $error"); } } ], ['eof' => sub{} ], ); } else{ $scp->expect($timeout_err, ['eof' => sub { }]); } if($verbose){ print $scp->after(),"\n" } $scp->hard_close(); return 1; } # Break the from/to line into its various parts sub _parse_scp_string{ my($self,$string) = @_; my @parts; my($user,$host,$dest); @parts = split(/@/,$string); if(scalar(@parts) == 2){ $user = shift(@parts); } else{ $user = $self->_get("user"); } my $temp = join('',@parts); ($host,$dest) = split(/:/,$temp); # scp('file','file') syntax, where local to remote is assumed unless($dest){ $dest = $host; $host = $self->_get("host"); } $host ||= $self->_get("host"); return ($user,$host,$dest); } sub reapChild{ do {} while waitpid(-1,WNOHANG) > 0; } 1; __END__ =head1 NAME Net::SCP::Expect - Wrapper for scp that allows passwords via Expect. =head1 SYNOPSIS B C<< my $scpe = Net::SCP::Expect->new; >> C<< $scpe->login('user name', 'password'); >> C<< $scpe->scp('file','host:/some/dir'); >> B C<< my $scpe = Net::SCP::Expect->new(host=>'host', user=>'user', password=>'xxxx'); >> C<< $scpe->scp('file','/some/dir'); # 'file' copied to 'host' at '/some/dir' >> B C<< my $scpe = Net::SCP::Expect->new(user=>'user',password=>'xxxx'); >> C<< $scpe->scp('host:/some/dir/filename','newfilename'); >> See the B method for more information on valid syntax. =head1 PREREQUISITES Expect 1.14. May work with earlier versions, but was tested with 1.14 (and now 1.15) only. Term::ReadPassword 0.01 is required if you want to execute the interactive test script. =head1 DESCRIPTION This module is simply a wrapper around the scp call. The primary difference between this module and I is that you may send a password programmatically, instead of being forced to deal with interactive sessions. =head1 USAGE Bnew(>Ival>,...B<)> Creates a new object and optionally takes a series of options (see OPTIONS below). =head2 METHODS B - Set this to 1 if you want to automatically pass a 'yes' string to any yes or no questions that you may encounter before actually being asked for a password, e.g. "Are you sure you want to continue connecting (yes/no)?" for first time connections, etc. BIB<)> This sets up an error handler to catch any problems with a call to 'scp()'. If you do not define an error handler, then a simple 'croak()' call will occur, with the last line sent to the terminal added as part of the error message. I highly recommend you forcibly terminate your program somehow within your handler (via die, croak, exit, etc), otherwise your program may hang, as it sits there waiting for terminal input. BIB<)> Sets the host for the current object BIB<)> If the login and password are not passed as options to the constructor, they must be passed with this method (or set individually - see 'user' and 'password' methods). If they were already set, this method will overwrite them with the new values. BIB<)> Sets the password for the current user BIB<)> Sets the user for the current object B Copies the file from source to destination. If no host is specified, you will be using 'scp' as an expensive form of 'cp'. There are several valid ways to use this method B BIB<);> BIB<);> # User already defined BIB<);> # User and host already defined BIB<);> # Same as previous B BIB<);> BIB<);> BI<:source, destination>B<);> =head1 OPTIONS B - Set this to 1 if you want to automatically pass a 'yes' string to any yes or no questions that you may encounter before actually being asked for a password, e.g. "Are you sure you want to continue connecting (yes/no)?" for first time connections, etc. B - Selects the cipher to use for encrypting the data transfer. B - Specify the host name. This is now useful for both local-to-remote and remote-to-local transfers. B - Specify the identify file to use. B - Set this to 1 if you want to turn off error checking. Use this if you're absolutely positive you won't encounter any errors and you want to speed up your scp calls - up to 2 seconds per call (based on the defaults). B