Module cwd assistance

91 views Asked by At

I created a Perl module that is to be used in many Perl scripts to use Net::SSH::Expect to do a login.

package myRoutines;
#
use v5.22;
use strict;
use warnings;

use Net::SSH::Expect;
use Exporter qw(import);
our @EXPORT_OK = qw(my_login);

sub my_login {
    my $user         = 'xxxx';
    my $port         = '10000';
    my $passwd       = 'XYZ';
    my $adminServer  = 'myServer';
    my $rootpassword = 'ABCDEF';

    my ( $pName, $vName ) = @_;

    our $ssh = Net::SSH::Expect->new(
        host       => "$adminServer",
        ssh_option => "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null",
        user       => "$user",
        password   => "$passwd",
        port       => "$port",
        raw_pty    => 1,
        restart_timeout_upon_receive => 1,
        log_file                     => "/var/tmp/clilog_$pName$vName"
    );

    eval {

        my $login_output = $ssh->login();

        if ( $login_output !~ />/ ) {
            die "Login has failed.
              Login output was $login_output";
        }
    };

    return $ssh;
}

1;

The scripts will do:

use myRoutines qw(my_login);

our ( $ssh, $pName, $vName );

$pName = 'abc';
$vName = '123';

$ssh = my_login( $pName, $vName );

$ssh->send( "some command\r" );

This all works if I'm in the directory that the script and module are in. If I'm in any other directory, the new call works but the call to $ssh->send does nothing.

I've tried adding to my script:

use lib '/some/dir'; 

(where the .pm file resides) to force it find the module, and that seems to work when I'm not in the directory where the pm file resides.

I've tried to:

use File::chdir;
$CWD = '/some/dir'; 

and again, the login seems to work but the next send does nothing. So I'm at a loss as to what might be happening and would like some advice.

Update 20170908:

Upon further playing and following the suggestions made, I've done the following and it now works:

removed the eval as it was unnecessary. removed the our's and made it my's. removed the ""'s

set the following in the script:

use File::Basename;
use Cwd qw( abs_path );
chdir "/some/dir";
use lib dirname(abs_path($0));
my $scriptName = basename($0);
use myRoutines qw(ovm_login);
my $pName = substr($scriptName,0,-3);   (cutting off the .pl from the end of the script name to pass the scriptname as the pName)

using chdir to change directory to where my pl script and pm file is and then setting the lib is seemingly working as it should.

Borodin, I'm not sure I understand your meaning when you say to object orient the module and .... but would be interested in hearing more to better understand.

2

There are 2 answers

0
Sarah Aziziyan On

The simple and easy way would be placing your .pm file in: /usr/lib64/perl5/ directory and you shouldn't have any problems. But still not the perfect solution, you should be able to put the .pm file wherever you want.

0
ikegami On

If you don't want to hardcode the directory, you can use

use FindBin qw( $RealBin );
use lib $RealBin;

($RealBin is the path to the script. Adjust as needed if myRoutines.pm is in a subdir.)