我是perl编程和网络的新手。我的任务是连接到远程服务器,并使用Perl脚本从服务器读取文件。我知道如何从本地机器读取文件,但不知道如何从远程服务器读取?
Code to read file from local machine but not know how to connect to remote server and read file?
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
open(FH, 'C:Userssaqib riazDesktopsaqiproperties.txt');
while(<FH>)
{
"$_";
}
close(FH);
我使用的是windows操作系统和padre ide的草莓最新版本
以下内容适用于Strawberry Perl 5.30版本。它使用Net::SSH2
并在服务器上打印文件file.txt
:
use strict;
use warnings;
use Net::SSH2;
my $host = 'my.host.com';
my $user = 'user_name';
my $password = 'xxxxxxx';
my $ssh2 = Net::SSH2->new();
$ssh2->connect($host) or $ssh2->die_with_error;
$ssh2->check_hostkey('ask') or $ssh2->die_with_error;
$ssh2->auth_password($user, $password);
my $chan = $ssh2->channel() or $ssh2->die_with_error;
$chan->exec('cat file.txt') or $ssh2->die_with_error;
print while <$chan>;
$chan->close;
注意:Net::SSH2
预装了Strawberry Perl,因此无需安装。
注意:我也尝试过Net::OpenSSH
,但无法使其工作。