在下面的代码中,我试图通过 ssh 连接到另一台机器并检查文件是否存在。
如果文件存在,那么我需要检查访问该文件的人是否是该文件的创建者,即他的用户名是否在日志文件中。
如果存在,那么我需要删除该文件。如何通过单次登录实现此目的,即用户只需输入一次密码
$user = $ENV{USER};
$il_check_cmd = "cat $shared_il_path/$il_name/info/.info_cat.log";
my $ssh_delete = Net::SSH::Perl->new($hostname, protocol => '1,2', debug => 0, interactive => 1) ;
$ssh_delete->login($username, $password);
($stdout,$stderr,$exit) = $ssh_delete->cmd("$il_check_cmd");
if((defined $stderr) && ($stderr =~ /No such file or directory/))
{
print "-E- $RUNCMD: \"$il_name\" you have entered does not exist in \"$shared_il_path\"!! !\n";
print "-E- $RUNCMD: or\n";
print "-E- $RUNCMD: \"$il_name\" does not contain \".info_cat.log\" file!!!\n";
print "-E- $RUNCMD: Exiting...\n";
exit;
}
@content = split(/ /,$stdout);
chomp($user_e = shift(@content));
if($user_e =~ /\b$user\b/)
{
print "This is the user who created the file";
//then remove the $shared_il_path/$il_name/info/.info_cat.log
}
最佳答案
考虑使用 Net::SSH::Expect
, 反而。链接页面包含 SSH session 的示例代码。
关于一个 session 中的 Perl ssh 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9563139/