-4

How to avoid the hard coded username/password in perl script

I am new to perl I have tried lot of things nothing worked out for me, Please help to provide some method to read the username/password from config file. What things I need to change here,

Below is my perl script:

#!/usr/bin/perl -w
use strict;
use warnings;
use Net::SFTP::Foreign;
my $server="sftp.abcpvt.com";
my $remote="outgoing_folder";
my $user="auser";
my $LOCAL_PATH="/home/sara";
my $file_transfer="DATA.ZIP";
my $password="abc123"
my %args = (user => "$user", password => "$password");
chdir $LOCAL_PATH or die "cannot cd to  ($!)\n";
my $sftp = Net::SFTP::Foreign->new(host=>$server,user=>$user,password=>$password) or die "unable to connect";
$sftp->error and die "SSH connection failed: " . $sftp->error;
$sftp->get("$remote/$file_transfer","$LOCAL_PATH/$file_transfer") or die "unable to retrieve file".$sftp->error;
undef $sftp;
exit;

MY config file contains below thing.

Username = “auser”;
Password = “abc123”;
Time_out=180;
Port = 22

I tried the below things,

my $user=get_credentials("/home/sar/config");
my $password=get_credentials("/home/sar/config");


sub get_credentials {
  my ($file) = @_;
  open my $fh, "<", $file or die $!;

  my $line = <$fh>;
  chomp($line);
  my ($user, $pass) = split /:/, $line;

  return ($user, password => $pass);
}

Here I am getting only the password, Username is not getting here…

Could you please share the sample coding for using the username/password in perl script.

user2767714
  • 51
  • 1
  • 4
  • 12
  • 3
    (1) If you didn't understand the answer here: http://stackoverflow.com/questions/18871267/how-to-read-the-password-from-the-text-file-in-perl either don't use it, or use it exactly as supplied, don't make random changes and be surprised it doesn't work. (2) What is "split" splitting a line on, what values does it expect to get out and does your config file match that format? But, do what Dave Cross says and use a config-file module (if not Config::Any then Config::Tiny or Config::Simple would be a good start). – Richard Huxton Sep 19 '13 at 10:32

1 Answers1

0

I think you want something like this.

sub get_config {
  my ($file) = @_;
  open my $fh, "<", $file or die $!;

  my %config;
  while (<>) {
    chomp;
    next unless /\S/;

    my ($key, $val) = split /\s*=\s*/;
    $val =~ s/^"//;
    $val =~ s/"$//;
    %config{$key} = $val;
  }
  return \%config;
}

my $config = get_config('/home/a911091/config');

my $user = $config->{Username};
my $pass = $config->{Password};

But you'd be far better off looking for a config file module from CPAN.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • i am getting error Can't locate config.pm in @INC (@INC contains: /usr/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi... Is it possible to read the username and password with out config module... Please help me dave – user2767714 Sep 19 '13 at 13:07
  • here i a going to use text file here instead of config to store the username/password/portno/time_out – user2767714 Sep 19 '13 at 13:13
  • "Can't locate config.pm in @INC". What is config.pm? Where did you get it from? Did you mean Config.pm? – Dave Cross Sep 19 '13 at 13:45
  • I don't think that comes even close to answering my questions. – Dave Cross Sep 19 '13 at 17:31