linux - 如何从perl中的文本文件中读取密码

标签 linux perl sftp perl-module password-protection

我是 perl 脚本的新手,下面的代码是从 sftp 服务器下载文件。

#!/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;

这里我硬编码了密码,我想避免在脚本中使用密码,有没有其他方法可以从文件中读取密码,或者有什么方法吗?

我在stackoverflow和google上搜索过,我不知道怎么用这个。我试过下面一个。

PASSWORD=`cat /home/sar/passwd_file.txt`

my $password = $ENV{'PASSWORD'}

你能帮我解决这个代码吗。

最佳答案

您可以将密码存储在具有有限权限的文件中,但使用 ssh key 仍然是更好的解决方案。

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;
}

如果你想将用户/密码存储在用分隔的文件中:你可以,

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);
}

关于linux - 如何从perl中的文本文件中读取密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18871267/

相关文章:

linux - Saxon 找不到 "jni.h"文件

perl - substr 在非常长的 UTF-8 字符串上的性能问题

linux - 在 SFTP 中传输文件之前检查远程目录大小

Java SFTP 客户端 - 概念

python - 如何使用Paramiko getfo从SFTP服务器下载文件到内存进行处理

c - 从 vcontainer 中销毁按钮

LinuxIPC : cannot remove msqid_ds

c++ - 将值作为函数参数传递还是计算两次?

windows - 当我从 Perl 调用命令时,如何抑制 "notify Microsoft"崩溃对话框?

Perl 根据命令行参数设置文件句柄