Catalyst-Model-SCP
view release on metacpan or search on metacpan
lib/Catalyst/Model/SCP.pm view on Meta::CPAN
my $success = $scp_client->is_connection_success;
warn 'SCP connection is good.' if $success;
my $success = $scp_client->put_file('local_file.txt','destination.txt');
warn 'File uploaded successfully' if $success;
my $success = $scp_client->download_file('destination.txt','local_file.txt');
warn 'File downloaded successfully' if $success;
=cut
has host => (
isa => 'Str',
is => 'ro',
required => 1,
);
has user => (
isa => 'Str',
is => 'ro',
required => 1,
);
has identity_file => (
isa => 'IdentityFile',
is => 'ro',
required => 1,
);
has net_scp_options => (
isa => 'HashRef',
is => 'ro',
default => sub {
return {};
}
);
has net_scp => (
isa => 'Net::SCP::Expect',
is => 'ro',
lazy_build => 1,
);
sub _build_net_scp {
my $self = shift;
my $other_options = $self->net_scp_options;
my %default_options = (
'host' => $self->host,
'user' => $self->user,
'identity_file' => $self->identity_file,
'auto_yes' => 1
);
my %options = (%{$other_options}, %default_options);
return Net::SCP::Expect->new(%options);
}
=head1 SUBROUTINES/METHODS
=head2 is_connection_success
C<is_connection_success()> - Returns true when it able to make connection using specified credentials.
=cut
has is_connection_success => (
isa => 'Bool',
is => 'ro',
lazy_build => 1
);
sub _build_is_connection_success {
my $self = shift;
my ($fh, $filename) = tempfile();
my $status = $self->put_file( $filename, '/home/'.$self->user.'/scp_connection_test' );
return $status;
}
=head2 put_file
C<put_file(LOCALFILE, REMOTEPATH)> - Transfer local file to the remote path.
Local file should have absolute path.
Remote file is the location on the remote server, no need to specify username, like normal scp.
=cut
sub put_file {
my( $self, $file, $destination ) = @_;
if ( not -f $file ){
warn "$file does not exist";
return 0;
}
my $scp_client = $self->net_scp;
my $status = 0;
eval {
$status = $scp_client->scp($file,$destination);
};
if ($@) {
warn $@;
}
return $status;
}
=head2 download_file
C<download_file(REMOTEFILE, LOCALPATH)> - Download remote file to local path.
Local file should have absolute path.
Remote file is the location on the remote server, no need to specify username, like normal scp.
=cut
sub download_file {
my( $self, $destination, $file ) = @_;
if ( not -e $file ){
warn "$file does not exist";
return 0;
}
my $scp_client = $self->net_scp;
my $status = 0;
eval {
my $destination_string = $self->user . '@' . $self->host .':'. $destination;
$status = $scp_client->scp($destination_string,$file);
};
( run in 1.524 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )