【发布时间】:2014-12-04 23:15:27
【问题描述】:
我需要
- 连接到远程服务器;那么
- 做一些事情,比如打开和读取文件的内容。
对于第 1 步:
my $server = "remoteservername.company.com";
my $ssh = Net::SSH::Perl->new("$server", debug => 1, protocol => 2, StrictHostKeyChecking => "no") or die "Error connecting server $server";
在终端输出消息
Connection established.
所以我假设我通过代码 ssh 连接到远程服务器。
对于第 2 步,我如何使用本地服务器的代码打开和读取远程服务器上的文件?这是迄今为止我能做的最好的:
use strict;
use warnings;
use diagnostics;
use warnings::register;
use Net::SSH::Perl;
use Net::SSH::Expect;
use Math::BigInt lib => "Calc,GMP,Pari";
my $server = "server09";
my $ssh = Net::SSH::Perl->new("$server", debug => 1, protocol => 2, StrictHostKeyChecking => "no") or die "Error connecting server $server";
#open(FILE, "/home/myid/f09.txt") || print("Unable to open test.o\n"); #works, on local, opens file[does not fail].
#open(FILE, "server09://home/myid/f09.txt") || print("Unable to open test.o\n"); #---> error: "Unable to open test.o"
my @remote_text = `this text is put into array.`;
my $remote_text = join ('',@remote_text);
open (FILE,'>/home/myid/f09.txt');
print FILE "$remote_text";
close (FILE);
exit(0);
然而,它不会向现有文件f09.txt 添加任何内容;如果我删除文件,open 也不会创建它。没有错误,但这似乎没有联系到远程文件。
只是对 ssh 的简单解释,然后从远程文件中读取会有所帮助。我看到的其他例子并没有削减它。当然,可能是我,漫长的一天,要离开它一段时间。非常感谢您的宝贵时间!
【问题讨论】:
-
如果我是你,我会编写 Perl 脚本在本地完成所有操作并通过 ssh 执行。