远程文件我的文件路径的数组:DIFF 2个使用Perl
@files = ('/home/.../file.txt', '/home/.../file2.txt',...);
我有多个远程计算机,具有相似的filestructure。我如何使用Perl来区分这些远程文件?
我想使用Perl反引号,ssh
和使用差异,但我有问题sh
(它不喜欢diff <() <()
)。
是否有一个很好的比较至少两个远程文件的Perl方法?
远程文件我的文件路径的数组:DIFF 2个使用Perl
@files = ('/home/.../file.txt', '/home/.../file2.txt',...);
我有多个远程计算机,具有相似的filestructure。我如何使用Perl来区分这些远程文件?
我想使用Perl反引号,ssh
和使用差异,但我有问题sh
(它不喜欢diff <() <()
)。
是否有一个很好的比较至少两个远程文件的Perl方法?
您可以使用名为Net :: SSH :: Perl的CPAN上的Perl模块来运行远程命令。
Link:从剧情简介http://metacpan.org/pod/Net::SSH::Perl
例子:
use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass);
my($stdout, $stderr, $exit) = $ssh->cmd($cmd);
你的命令看起来像
my $cmd = "diff /home/.../file.txt /home/.../file2.txt";
编辑:这些文件在不同的服务器。
您仍然可以使用Net :: SSH :: Perl来读取文件。
#!/bin/perl
use strict;
use warnings;
use Net::SSH::Perl;
my $host = "First_host_name";
my $user = "First_user_name";
my $pass = "First_password";
my $cmd1 = "cat /home/.../file1";
my $ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass);
my($stdout1, $stderr1, $exit1) = $ssh->cmd($cmd1);
#now stdout1 has the contents of the first file
$host = "Second_host_name";
$user = "Second_user_name";
$pass = "Second_password";
my $cmd2 = "cat /home/.../file2";
$ssh = Net::SSH::Perl->new($host);
$ssh->login($user, $pass);
my($stdout2, $stderr2, $exit2) = $ssh->cmd($cmd2);
#now stdout2 has the contents of the second file
#write the contents to local files to diff
open(my $fh1, '>', "./temp_file1") or DIE "Failed to open file 1";
print $fh1 $stdout1;
close $fh1;
open(my $fh2, '>', "./temp_file2") or DIE "Failed to open file 2";
print $fh2 $stdout2;
close $fh2;
my $difference = `diff ./temp_file1 ./temp_file2`;
print $difference . "\n";
我还没有测试过这段代码,但是你可以这样做。记得下载Perl模块Net :: SSH :: Perl来运行远程命令。
差异不在Perl核心模块中实现,但另一个名为CPAN上的Text :: Diff,所以也许这也可以。希望这可以帮助!
问题是,我试图区分两个不同服务器中的两个文件,而不是同一台服务器中的两个文件。 – jh314
@ jh314请参阅编辑。网络:SSH仍然可以工作,但你必须使用两次才能使用这两台机器。 – chilemagic
谢谢!创建临时文件真的有必要吗?其中一些文件非常大! – jh314
使用rsync
到远程文件复制到本地计算机,然后使用diff
找出差异:
use Net::OpenSSH;
my $ssh1 = Net::OpenSSH->new($host1);
$ssh1->rsync_get($file, 'master');
my $ssh2 = Net::OpenSSH->new($host2);
system('cp -R master remote');
$ssh2->rsync_get($file, 'remote');
system('diff -u master remote');
使用'如果你想使用'bash' <(...)'语法。 – mob