create perl script to connect remotely to another server and read File?

▼魔方 西西 提交于 2020-08-08 07:00:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!