Configuring Perl modules to use Secure File Transfer Protocol (SFTP) in IBM AIX

Introduction

While working in IBM® AIX®, sometimes, you may come across situations where you need to use a Perl module in your Perl program. For this, you need to install and configure the Perl module package before using it in your Perl program.

A use case scenario is created to run a program in IBM AIX with and without the Perl module. It has been observed that the program ran successfully with the Perl module and its dependencies installed.

Along with the general Perl module, this tutorial focuses on how to use the Perl module and its dependencies in user-level programming.

This tutorial provides generic sample programs that can enable IBM AIX Perl programmers to meet their specific requirements with minor modifications.

Prerequisites to write a Perl program

  • Knowledge of C programming language with basic AIX operating system.
  • XLC compiler to compile the sample programs.

Prerequisites to install a Perl module on AIX

Install the following components before installing and configuring a Perl module:

  1. Install xlC and vac based on the requirement.

    The xlC package is in built with AIX. This is available by default at the time of AIX installation. Confirm installation using the following command:

    lslpp -l xlC *
    

    Figure 1. Listing xlC

    Listing xlC

    If it is not installed, you can download it from IBM Fix Central and follow the Readme file for the installation instructions. Because it is a licensed product from IBM, you should have access to Passport Advantage to download it.

    IBM Fix Central: https://www-945.ibm.com/support/fixcentral/

    Figure 2. Finding xlC to download

    Figure 2. Finding xlC to download

  2. Add the xlC home folder /usr/vacpp/bin to PATH in the .profile file of the root user. Check the xlC home folder using the which xlc command:

    xlC home folder

  3. Edit the .profile file of the root user using the vi .profile command and add the /usr/vacpp/bin/xlc PATH.

    .profile file of the root user

Install SFTP Perl module

Perform the following steps to install a Perl module on an AIX server (ittakes approximately 40 to60 minutes to complete installation including tests):

  1. Install the required Perl module and its dependencies.

    perl -MCPAN -e shell
    cpan[1]> force install <module name>
    

    Example: force install File::Basename

    Install the following dependencies as a prerequisite for step 2.

    • File::Basename: ‘0’
    • File::Copy: ‘0’
    • File::Spec: ‘0’
    • IO::Socket::INET: ‘0’
    • IO::Socket::SSL: ‘1.26’
    • Net::HTTPTunnel: ‘0.5’
    • Net::SSLeay: ‘0’
    • Net::SSLeay::Handle: ‘0’
    • Sys::Hostname: ‘0’
    • Test::More: ‘0.88’
    • Time::Local: ‘0’
  2. Install the Net::FTPSSL for FTPS Perl module.

    Run the perl command as a root user and enter the Comprehensive Perl Archive Network (CPAN) prompt to install the module.

    perl -MCPAN -e shell
    

    Run the below force command to install FTPSSL module.

    cpan[2]> force install Net::FTPSSL
    

    Output Log of the NET::FTPSSL Installation

    Running install for module 'Net::FTPSSL'
    Running make for C/CL/CLEACH/Net-FTPSSL-0.41.tar.gz
    Checksum for /u/root/.cpan/sources/authors/id/C/CL/CLEACH/Net-FTPSSL-0.41.tar.gz ok
    Net-FTPSSL-0.41/
    Net-FTPSSL-0.41/Changes
    Net-FTPSSL-0.41/FTPSSL.pm
    Net-FTPSSL-0.41/LICENSE
    Net-FTPSSL-0.41/MANIFEST
    Net-FTPSSL-0.41/META.json
    Net-FTPSSL-0.41/META.yml
    Net-FTPSSL-0.41/Makefile.PL
    Net-FTPSSL-0.41/README
    Net-FTPSSL-0.41/t/
    Net-FTPSSL-0.41/t/00-basic.t
    Net-FTPSSL-0.41/t/01-test-if-environment-ok.t
    Net-FTPSSL-0.41/t/02-ask-questions.t
    Net-FTPSSL-0.41/t/03-ask-questions-again.t
    Net-FTPSSL-0.41/t/05-check-new-cmd.t
    Net-FTPSSL-0.41/t/06-login.t
    Net-FTPSSL-0.41/t/07-prompt_validation.t
    Net-FTPSSL-0.41/t/20-test_multiple_connections.t
    Net-FTPSSL-0.41/t/22-supported.t
    Net-FTPSSL-0.41/t/24-dir_tests.t
    Net-FTPSSL-0.41/t/26-ccc_test.t
    Net-FTPSSL-0.41/t/28-put_get_delete_tests.t
    Net-FTPSSL-0.41/t/30-more_put_delete_tests.t
    Net-FTPSSL-0.41/t/32-rename_tests.t
    Net-FTPSSL-0.41/t/34-xput_tests.t
    Net-FTPSSL-0.41/t/36-quot_and_callback.t
    Net-FTPSSL-0.41/t/data/
    Net-FTPSSL-0.41/t/data/00-basic.txt
    Net-FTPSSL-0.41/t/data/test_file.tar.gz
    Net-FTPSSL-0.41/t/logs/
    Net-FTPSSL-0.41/t/logs/abc
    Net-FTPSSL-0.41/t/test-helper/
    Net-FTPSSL-0.41/t/test-helper/helper1234.pm
    Net-FTPSSL-0.41/t/work/
    Net-FTPSSL-0.41/t/work/abc
    CPAN.pm: Going to build C/CL/CLEACH/Net-FTPSSL-0.41.tar.gz
    Checking if your kit is complete...
    Looks good
    Writing Makefile for Net::FTPSSL
    cp FTPSSL.pm blib/lib/Net/FTPSSL.pm
    Manifying blib/man3/Net::FTPSSL.3
    CLEACH/Net-FTPSSL-0.41.tar.gz
    /usr/bin/make -- OK
    Running make test
    PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
    t/00-basic.t ...................... ok
    t/01-test-if-environment-ok.t ..... ok
    t/02-ask-questions.t .............. 1/? #
    # Configuring the test cases
    # Some information will be required for running any FTPS tests:
    # A secure ftps server address, a user, a password and a directory
    # where the user has permissions to read and/or write files to.
    # Hopefully only the Net::FTPSSL tests have access to to this dir.
    #
    # Do you want to run the server connectivity tests ? (Y|N) [N]
    # Skipping all tests per user request ...
    t/02-ask-questions.t .............. ok
    t/03-ask-questions-again.t ........ ok
    t/05-check-new-cmd.t .............. ok
    t/06-login.t ...................... ok
    t/07-prompt_validation.t .......... ok
    t/20-test_multiple_connections.t .. ok
    t/22-supported.t .................. ok
    t/24-dir_tests.t .................. ok
    t/26-ccc_test.t ................... ok
    t/28-put_get_delete_tests.t ....... ok
    t/30-more_put_delete_tests.t ...... ok
    t/32-rename_tests.t ............... ok
    t/34-xput_tests.t ................. ok
    t/36-quot_and_callback.t .......... ok
    All tests successful.
    Files=16, Tests=53, 73 wallclock secs ( 0.14 usr  0.04 sys +  1.92 cusr  0.37 csys =  2.47 CPU)
    Result: PASS
    Target "test" is up to date.
    CLEACH/Net-FTPSSL-0.41.tar.gz
    /usr/bin/make test -- OK
    Running make install
    Prepending /u/root/.cpan/build/Net-FTPSSL-0.41-kvCB_p/blib/arch /u/root/.cpan/build/Net-FTPSSL-0.41-kvCB_p/blib/lib to  PERL5LIB for 'install'
    Installing /usr/opt/perl5/lib/site_perl/5.10.1/Net/FTPSSL.pm
    Appending installation info to /usr/opt/perl5/lib/5.10.1/aix-thread-multi/perllocal.pod
    CLEACH/Net-FTPSSL-0.41.tar.gz
    /usr/bin/make install  -- OK
    ****End of the output log****
    
  3. Open the required ports in firewall if applicable. In this tutorial, it is considered that all the ports are open in firewall.

  4. Change the version of the Perl module IO::Socket::SSL currently installed on the server in /usr/opt/perl5/lib/site_perl/5.10.1/Net/FTPSSL.pm file. If version is prior to 2.060, then change it to 2.060 by editing the FTPSSL.pm file.

    Example:

    grep IO::Socket::SSL /usr/opt/perl5/lib/site_perl/5.10.1/Net/FTPSSL.pm
    use IO::Socket::SSL 2.060;
    
  5. Change the version of the Perl module Net::SSLeay currently installed on the server in /usr/opt/perl5/lib/site_perl/5.10.1/IO/Socket/SSL.pm file. If version is prior to 1.85, then change it to 1.85 by editing the SSL.pm file.

    Example:

    grep Net::SSLeay /usr/opt/perl5/lib/site_perl/5.10.1/IO/Socket/SSL.pm
    use Net::SSLeay 1.85;
    
  6. Check the ownership of the user who has root access to run the script (mostly user need root permission to run the script). Change the directory to /usr/opt/perl5/lib/site_perl/5.10.1 and provide full permission to users, read permission to groups, and execute permission to others.

    cd /usr/opt/perl5/lib/site_perl/5.10.1
    chmod -R 755 *
    

    Example:

    Before changing the permission:

    root@Hostname:/usr/opt/perl5/lib/site_perl/5.10.1
    => ls -lrt
    total 496
    -r--r--r--    1 root     system        25412 Oct 19 2011  lwptut.pod
    -r--r--r--    1 root     system         9119 Oct 19 2011  lwpcook.pod
    -r--r--r--    1 root     system        29975 Oct 19 2011  URI.pm
    -r--r--r--    1 root     system        19400 Oct 19 2011  LWP.pm
    -r--r--r--    1 root     system         4802 Sep 27 2013  Digest.pm
    drwxr-xr-x    4 root     system         4096 Mar 11 2014  URI
    drwxr-xr-x    2 root     system          256 Mar 11 2014  HTML
    drwxr-xr-x    4 root     system         4096 Mar 11 2014  LWP
    drwxr-xr-x    5 root     system         4096 Mar 11 2014  HTTP
    drwxr-xr-x    4 root     system          256 Mar 11 2014  XML
    drwxr-xr-x    3 root     system          256 Mar 11 2014  WWW
    drwxr-xr-x    3 root     system          256 Mar 11 2014  IO
    drwxr-xr-x    2 root     system          256 Mar 11 2014  File
    drwxr-xr-x    2 root     system          256 Mar 11 2014  Bundle
    -r--r--r--    1 root     system        18181 Aug 16 2014  Spiffy.pod
    -r--r--r--    1 root     system        15485 Aug 16 2014  Spiffy.pm
    -r--r--r--    1 root     system        58003 Mar 13 2018  SSLeay.pm
    -r--r--r--    1 root     system        22634 Nov  3 13:01 YAML.pod
    -r--r--r--    1 root     system         3163 Nov  3 13:01 YAML.pm
    drwx------    3 root     system          256 Dec 17 10:17 Mozilla
    drwx------    2 root     system          256 Jan 10 14:01 Algorithm
    drwx------    3 root     system          256 Jan 10 14:02 Text
    drwx------    2 root     system          256 Jan 10 14:02 Spiffy
    drwx------    4 root     system          256 Jan 10 14:03 Test
    drwx------    4 root     system         4096 Jan 10 14:03 YAML
    drwxr-xr-x    3 root     system         4096 Jan 10 15:29 Net
    drwx------    2 root     system          256 Jan 10 15:53 Module
    drwx------    3 root     system          256 Jan 10 15:55 JSON
    drwx------    3 root     system          256 Jan 10 15:55 CPAN
    drwx------    2 root     system          256 Jan 10 15:56 Perl
    drwxrwxrwx    8 root     system         4096 Jan 14 12:27 aix-thread-multi
    

    After changing the permission:

    root@Hostname:/usr/opt/perl5/lib/site_perl/5.10.1
    => ls -lrt
    total 496
    -rwxr-xr-x    1 root     system        25412 Oct 19 2011  lwptut.pod
    -rwxr-xr-x    1 root     system         9119 Oct 19 2011  lwpcook.pod
    -rwxr-xr-x    1 root     system        29975 Oct 19 2011  URI.pm
    -rwxr-xr-x    1 root     system        19400 Oct 19 2011  LWP.pm
    -rwxr-xr-x    1 root     system         4802 Sep 27 2013  Digest.pm
    drwxr-xr-x    4 root     system         4096 Mar 11 2014  URI
    drwxr-xr-x    2 root     system          256 Mar 11 2014  HTML
    drwxr-xr-x    4 root     system         4096 Mar 11 2014  LWP
    drwxr-xr-x    5 root     system         4096 Mar 11 2014  HTTP
    drwxr-xr-x    4 root     system          256 Mar 11 2014  XML
    drwxr-xr-x    3 root     system          256 Mar 11 2014  WWW
    drwxr-xr-x    3 root     system          256 Mar 11 2014  IO
    drwxr-xr-x    2 root     system          256 Mar 11 2014  File
    drwxr-xr-x    2 root     system          256 Mar 11 2014  Bundle
    -rwxr-xr-x    1 root     system        18181 Aug 16 2014  Spiffy.pod
    -rwxr-xr-x    1 root     system        15485 Aug 16 2014  Spiffy.pm
    -rwxr-xr-x    1 root     system        58003 Mar 13 2018  SSLeay.pm
    -rwxr-xr-x    1 root     system        22634 Nov  3 13:01 YAML.pod
    -rwxr-xr-x    1 root     system         3163 Nov  3 13:01 YAML.pm
    drwxr-xr-x    3 root     system          256 Dec 17 10:17 Mozilla
    drwxr-xr-x    2 root     system          256 Jan 10 14:01 Algorithm
    drwxr-xr-x    3 root     system          256 Jan 10 14:02 Text
    drwxr-xr-x    2 root     system          256 Jan 10 14:02 Spiffy
    drwxr-xr-x    4 root     system          256 Jan 10 14:03 Test
    drwxr-xr-x    4 root     system         4096 Jan 10 14:03 YAML
    drwxr-xr-x    3 root     system         4096 Jan 10 15:29 Net
    drwxr-xr-x    2 root     system          256 Jan 10 15:53 Module
    drwxr-xr-x    3 root     system          256 Jan 10 15:55 JSON
    drwxr-xr-x    3 root     system          256 Jan 10 15:55 CPAN
    drwxr-xr-x    2 root     system          256 Jan 10 15:56 Perl
    drwxr-xr-x    8 root     system         4096 Jan 14 12:27 aix-thread-multi
    

Test SFTP using perl

You need to test the SFTP connectivity between the source and target servers. Here I’ve given the sample script to test it.

Attention: Check with application team for the actual script to be run to check the jobs.

Syntax to run the script:

perl <location of the script> <ftp server name> <User ID> <password>

Sample script:

#!/usr/bin/perl -w

#
#use strict;
#use warnings;
use Net::FTPSSL;
#use File::Basename;

# Perform the ftp
$ftps=Net::FTPSSL->new($host,Debug  => 1);
$ftps->login($ftp_id,$ftp_pwd) or die "Fatal: I could not login to the ftp host.\n";
$ftps->binary;
$ftps->site($site_cmd) or die "Fatal: I could not send site command $site_cmd.\n";
$ftps->quit();
print "ftp completed\n";
#End of script

Summary

Perl modules are used in Perl scripts. Many AIX users are interested in having these modules on AIX so that they can write their own Perl scripts. This tutorial provides an easy way to install all these Perl modules.

Gobinath Panchavarnam