如何从Perl中的文本文件读取密码 [英] How to read the password from the text file in perl
问题描述
我是perl脚本的新手,以下代码是从sftp服务器下载文件.
I am new to perl script, The below code is downloading the files from sftp server.
#!/usr/bin/perl
use strict;
use warnings;
use Net::SFTP::Foreign;
my $sftp = Net::SFTP::Foreign->new(
'auser@sftp.rent.com',
password => 'auser123',
more => ['-v']
);
$sftp->get('outgoing/DATA.ZIP', '/home/sar/')
or die "unable to retrieve copy: ".$sftp->error;
$sftp->disconnect;
在这里,我已经很努力地输入了密码,我想避免在脚本中输入密码,是否有其他方法可以从文件中读取密码,或者有任何方法?
Here i have hard codded the password, I want to avoid password in the script, Is there any other method to read the password from the file or any method is there?
我在stackoverflow和google中进行了搜索,我不知道该如何使用.我尝试了一个以下.
I searched in the stackoverflow and google, i don't know how to use this. I tried below one.
PASSWORD=`cat /home/sar/passwd_file.txt`
my $password = $ENV{'PASSWORD'}
请帮助我解决此代码.
推荐答案
您可以将密码存储在权限有限的文件中,但是使用ssh密钥仍然是更好的解决方案.
You can store password in file with limited permissions, but using ssh keys is still better solution.
my $sftp = Net::SFTP::Foreign->new(
'auser@sftp.rent.com',
password => get_passw("chmod_600_passw_file"),
more => ['-v']
);
sub get_passw {
my ($file) = @_;
open my $fh, "<", $file or die $!;
my $pass = <$fh>; # do { local $/; <$fh> };
chomp($pass);
return $pass;
}
如果您想将用户名/通行证都存储在以:
分隔的文件中,则可以
If you want to store both user/pass in file separated with :
you can,
my $sftp = Net::SFTP::Foreign->new(
get_credentials("chmod_600_passw_file"),
more => ['-v']
);
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);
}
这篇关于如何从Perl中的文本文件读取密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!