Bugzilla – Attachment 166085 Details for
Bug 36766
Add command-line utility to SFTP a file to a remote server
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 36766: WIP Command-line utility to SFTP file to remote server
Bug-36766-WIP-Command-line-utility-to-SFTP-file-to.patch (text/plain), 6.43 KB, created by
Alex Buckley
on 2024-05-02 20:34:00 UTC
(
hide
)
Description:
Bug 36766: WIP Command-line utility to SFTP file to remote server
Filename:
MIME Type:
Creator:
Alex Buckley
Created:
2024-05-02 20:34:00 UTC
Size:
6.43 KB
patch
obsolete
>From 52a9b4751db783bd7a5faab22a23038edfedff8f Mon Sep 17 00:00:00 2001 >From: Alex Buckley <alexbuckley@catalyst.net.nz> >Date: Fri, 3 May 2024 06:50:19 +1200 >Subject: [PATCH] Bug 36766: WIP Command-line utility to SFTP file to remote > server > >--- > misc/cronjobs/sftp_file.pl | 218 +++++++++++++++++++++++++++++++++++++ > 1 file changed, 218 insertions(+) > create mode 100755 misc/cronjobs/sftp_file.pl > >diff --git a/misc/cronjobs/sftp_file.pl b/misc/cronjobs/sftp_file.pl >new file mode 100755 >index 00000000000..a6936267eed >--- /dev/null >+++ b/misc/cronjobs/sftp_file.pl >@@ -0,0 +1,218 @@ >+#!/usr/bin/perl >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it >+# under the terms of the GNU General Public License as published by >+# the Free Software Foundation; either version 3 of the License, or >+# (at your option) any later version. >+# >+# Koha is distributed in the hope that it will be useful, but >+# WITHOUT ANY WARRANTY; without even the implied warranty of >+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the >+# GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License >+# along with Koha; if not, see <http://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+use C4::Context; >+use Net::SFTP::Foreign; >+use C4::Log qw( cronlogaction ); >+use Koha::Email; >+use Getopt::Long qw( GetOptions ); >+use Pod::Usage qw( pod2usage ); >+use Carp qw( carp ); >+ >+=head1 NAME >+ >+sftp_file.pl - SFTP a file to a remote server >+ >+=head1 SYNOPSIS >+ >+oclc.pl [ -h | -m ] [ -v ] method host user pass upload_dir file >+ >+ Options: >+ -h --help brief help message >+ -m --man full documentation, same as --help --verbose >+ -v --verbose verbose output >+ --host SFTP host to upload to >+ --user SFTP user >+ --pass SFTP password >+ --upload_dir Directory on SFTP host to upload to >+ --file File to SFTP to host >+ --email Email address to receive confirmation of SFTP upload success or failure >+ >+=head1 OPTIONS >+ >+=over >+ >+=item B<--help> >+ >+Print brief help and exit. >+ >+=item B<--man> >+ >+Print full documentation and exit. >+ >+=item B<--verbose> >+ >+Increased verbosity, reports successes and errors. >+ >+=item B<--host> >+ >+Remote server host to SFTP file to. >+ >+=item B<--user> >+ >+Username credential to authenticate to remote host. >+ >+=item B<--pass> >+ >+Password credential to authenticate to remote host. >+ >+=item B<--upload_dir> >+ >+Directory on remote host to SFTP file to. >+ >+=item B<--file> >+ >+File to SFTP to remote host. >+ >+=item B<--email> >+ >+Email address to receive the confirmation if the SFTP upload was a success or failure. >+ >+=back >+ >+=head1 DESCRIPTION >+ >+This script is designed to SFTP files. >+ >+=head1 DESCRIPTION >+ >+This script is designed to SFTP files to a remote server. >+ >+=head1 USAGE EXAMPLES >+ >+B<sftp_file.pl --host test.com --user test --pass 123cne --upload_dir uploads --file /tmp/test.mrc> >+ >+In this example the script will upload /tmp/test.mrc on the Koha server to the test.com remote host using the username:password of test:123cne. The file will be SFTP to the uploads directory. >+ >+=head1 TO DO >+ >+=back >+ >+=cut >+ >+# These variables can be set by command line options, >+# initially set to default values. >+ >+my $help = 0; >+my $man = 0; >+my $verbose = 0; >+my $host = undef; >+my $user = undef; >+my $pass = undef; >+my $upload_dir = undef; >+my $file = 0; >+my $email = 0; >+my $admin_address = undef; >+my $status_email = undef; >+my $status_email_message = undef; >+my $sftp_status = undef; >+ >+my $command_line_options = join(" ",@ARGV); >+ >+GetOptions( >+ 'help|?' => \$help, >+ 'man' => \$man, >+ 'verbose' => \$verbose, >+ 'host=s' => \$host, >+ 'user=s' => \$user, >+ 'pass=s' => \$pass, >+ 'upload_dir=s' => \$upload_dir, >+ 'file=s' => \$file, >+ 'email=s' => \$email, >+) or pod2usage(2); >+pod2usage( -verbose => 2 ) if ($man); >+pod2usage( -verbose => 2 ) if ($help and $verbose); >+pod2usage(1) if $help; >+ >+cronlogaction({ info => $command_line_options }); >+ >+# Check we have all the SFTP details we need >+if ( !$user || !$pass || !$host || !$upload_dir ) { >+ pod2usage(q|Please provide details for sftp username, password, upload directory, and host|); >+} >+ >+# Prepare the SFTP connection >+my $sftp = Net::SFTP::Foreign->new( >+ host => $host, >+ user => $user, >+ password => $pass, >+ timeout => 10, >+ stderr_discard => 1, >+); >+$sftp->die_on_error( "Cannot ssh to $host" . $sftp->error ); >+ >+# Change to remote directory >+$sftp->setcwd( $upload_dir ) >+ or return warn "Cannot change remote dir : $sftp->error\n"; >+ >+# If --mail parameter is defined then prepare sending an email confirming the success >+# or failure of the SFTP >+if ( $email ) { >+ if ( C4::Context->preference('KohaAdminEmailAddress') ) { >+ $admin_address = C4::Context->preference('KohaAdminEmailAddress'); >+ } >+ >+ if ( !Koha::Email->is_valid($email) ) { >+ return warn "The email address you defined in the --email parameter is invalid\n"; >+ } >+ >+ $status_email = Koha::Email->new( >+ { >+ to => $email, >+ from => $admin_address, >+ subject => '[OCLC] ' . C4::Context->config('database') . ' ' . $sftp_status, >+ } >+ ); >+} >+ >+# Do the SFTP upload >+open my $fh, '<', $file; >+if ( >+ $sftp->put( >+ $fh, $file, >+ callback => sub { my ( $sftp, $data, $offset, $size ) = @_; warn "$offset of $size bytes transferred\n"; } >+ ) >+) { >+ $sftp_status = 'successful'; >+ # Send success email >+ if ( $email ) { >+ $status_email_message = "OCLC upload was successful"; >+ } >+ close $fh; >+} else { >+ $sftp_status = 'failed'; >+ # Send failure email >+ if ( $email ) { >+ $status_email_message = "OCLC upload failed\nOCLC error: " . $sftp->error; >+ } >+} >+ >+# Send email confirming the success or failure of the SFTP >+if ( $email ) { >+ $status_email->text_body($status_email_message); >+ my $smtp_server = Koha::SMTP::Servers->get_default; >+ $email->transport( $smtp_server->transport ); >+ try { >+ $email->send_or_die; >+ } >+ catch { >+ carp "Mail not sent: $_"; >+ }; >+} >+ >+cronlogaction({ action => 'End', info => "COMPLETED" }); >-- >2.30.2
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 36766
:
166085
|
166089
|
166471
|
166859
|
172151
|
172152
|
172153
|
172154
|
172161
|
172162
|
172163
|
172164
|
172176
|
172177
|
172178
|
172179
|
172180
|
172181
|
173596