问题
I am new to perl programming also to networking . My Task is to connect to remote server and read file from server using Perl script.I know how to read file from local machine but not how to read from remote server?
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:\Users\saqib riaz\Desktop\saqi\properties.txt');
while(<FH>)
{
"$_";
}
close(FH);
I am using window operating system And strawberry with padre ide latest version
回答1:
The following works for me with Strawberry Perl version 5.30. It uses Net::SSH2 and prints a file file.txt
on the server:
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;
Note: Net::SSH2
comes preinstalled with Strawberry Perl so no need to install it.
Note: I also tried Net::OpenSSH but could not get it to work.
来源:https://stackoverflow.com/questions/63166325/create-perl-script-to-connect-remotely-to-another-server-and-read-file