【问题标题】:Check Whether Directory Exists In Remote Server For Perl检查 Perl 远程服务器中是否存在目录
【发布时间】:2018-12-19 21:13:10
【问题描述】:

我希望检查给定的路径是否存在或者是否是 Perl 中另一个站点服务器中的目录。我的代码如下。

my $destination_path = "<path>";
my $ssh = "usr/bin/ssh";
my $user_id = getpwuid( $< );
my $site = "<site_name>";
my $host = "rsync.$site.com";

if ($ssh $user_id\@$host [-d $destination_path]){
  print "Is a directory.\n";
}
else{
  print "Is not a directory.\n";
}

我确定我的代码是错误的,因为我根据我从另一个问题中看到的 bash 示例修改了代码,但我不知道如何修复它。感谢所有在这里提供帮助的人。

【问题讨论】:

  • 你好像忘了提问题出在哪里。你看到了什么意外的行为?在我怀疑你想要/usr/bin/ssh 的地方拥有usr/bin/ssh 是否那么简单?
  • 嗨。 @DaveCross 我的代码问题不适用于检查远程服务器中是否存在目录。

标签: linux perl ssh


【解决方案1】:

[是命令名,在命令行中必须与其他单词分开。所以只需使用更多的空格:

$ssh $user\@$host [ -d $destination_path ]

要实际执行此命令,您需要使用内置的system 函数。 system 执行命令成功时返回 0(请参阅链接中的文档)

if (0 == system("$ssh $user\@$host [ -d $destination_path ]")) {
    print "Is a directory.\n";
} else {
    print "Is not a directory.\n";
}

【讨论】:

  • 嗨。感谢您的回答。答案是有效的。请问如果路径不是目录,如何为路径创建目录?我试过system("$ssh $user\@$host [ mkdir -p $destination_path ]"),但它似乎不起作用。
【解决方案2】:

通过SFTP访问远程文件系统:

use Net::SFTP::Foreign;

$sftp = Net::SFTP::Foreign->new("rsync.$site.com");
if ($sftp->test_d($destination_path)) {
    "print $destination_path is a directory!\n";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-22
    • 2020-06-23
    • 1970-01-01
    • 2019-06-09
    • 2013-01-16
    • 1970-01-01
    • 2015-07-26
    • 2012-06-13
    相关资源
    最近更新 更多