Net::SFTP Hints from IP Offices

Whenever we receive a hint or tip from IP offices concerning any of the PCT EDI software or services, we publish it for the use of others.

A Simple Perl Script Using Net::SFTP

Description

This simple demo program was taken from the Net::SFTP documentation and modified slightly by Jim Fullton at WIPO. To use this example, you must first have set up a correct OpenSSH user installation as described in the Users Guide, with working public key authentication. As with the other examples, replace "xx" with your account name. This demonstration is based upon sample code provided with the Net::SFTP module, and will simply print a directory listing of the user account, and then exit. As outlined in the Net::SFTP documentation, the functions to 'Get' (sftp->Get()) a file from the server and 'Put' (sftp->Put()) a file on the server are almost identical to the 'ls' (sftp->ls()) request implemented by the example below.

How To.....

#!/usr/bin/perl -w
############################################################
#
# demo1.pl - demonstrate the basic concepts of using the
# Net::SFTP Perl module with the WIPO PCT-EDI server.
#
# Makes a connection and returns a directory listing
# Requires a properly configured OpenSSH public key authentication
# environment for user "xx", where xx is the 2 letter WIPO ST.3
# country code.
#
# May 27, 2004 Jim Fullton. A slight modification of one of
# the examples in the Net::SFTP documentation
#
# Arguments: demo1.pl -v -u=username
# Both are optional. If -u username is not provided
# the current username for the account is used
#
# You must be using public key encryption for this example
# to work. This is the first of many examples.
#
# ./demo1.pl pctftp.wipo.int -u xx
#
############################################################
use strict;
use Net::SFTP;
use Getopt::Long;
my %opts;
my $user;
Getopt::Long::Configure('no_ignore_case');
GetOptions(\%opts, "v", 'u=s'=>\$user);
my($host) = @ARGV;
die "usage: demo1 [options] hostname" unless $host;
# set up the arguments based on the command line options
my %args = (ssh_args => []);
$args{debug} = 1 if $opts{v};
push @{ $args{ssh_args} }, user => $user ;
# make our connection
print "Connecting to $host...\n";
my $sftp = Net::SFTP->new($host, %args);
# get a listing of the base directory
$sftp->ls("." , sub { print $_[0]->{longname}, "\n" });
print "Finished\n";