[Perl][net::ssh2] 如何在执行远程命令时保持 ssh 连接 [英] [Perl][net::ssh2] How to keep the ssh connection while executing remote command

查看:71
本文介绍了[Perl][net::ssh2] 如何在执行远程命令时保持 ssh 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用 net::ssh2 的 perl 脚本来建立到远程服务器的 SSH 连接.(我在 Windows 上工作)

I'm working on a perl script using net::ssh2 to make a SSH connection to a remote server. (I'm working on windows)

我选择 Net::SSH2 是因为我必须在同一个脚本中建立一些 SFTP 连接.

I chose Net::SSH2 because i had to make some SFTP connections in the same script.

目前,我的 sftp 连接工作正常.问题是当我尝试执行长时间"命令时.我的意思是一个执行时间可能超过 30 秒的命令.

For now, my sftp connections work perfectly. The problem is when i try to execute a "long-duration" command. I mean a command which execution can take more than 30sec.

    $ssh2 = Net::SSH2->new();
    $ssh2->connect('HOST') or die;
    if($ssh2->auth(username=>'USER', password=>'PSWD'))
    {
        $sftp = Net::SFTP::Foreign->new(ssh2=>$ssh2, backend=>'Net_SSH2');
        $sftp->put('local_path', 'remote_path');
        $channel=$ssh2->channel();
    ##
        $channel->shell('BCP_COMMAND_OR_OTHER_PERL_SCRIPT');
    # OR (I tried both, both failed :( )
        $channel->exec('BCP_COMMAND_OR_OTHER_PERL_SCRIPT');
    ##
        $channel->wait_closed();
        $channel->close();
        print "End of command";
        $sftp_disconnect();
    }
    $ssh2->disconnect();

当我执行此脚本时,连接成功,文件已正确发送,但未(完全)执行执行.我的意思是,我认为该命令已发送执行但立即终止或根本不发送,我不确定.

When i execute this script, the connection is successfull, the file is correctly sent but the execution is not (completely) performed. I mean, I think the command is sent for execution but terminated immediatly or not sent at all, i'm not sure.

我想要的是脚本在断开所有连接之前等待命令完全完成(只是因为有时,我需要获取命令执行的结果)

What i want is the script waits until the command is completly finished before disconnect everything (just because sometimes, i need to get the result of the command execution)

有大佬知道怎么解决吗?:( cpan 文档对此不是很明确

Does anyone know how to solve this? :( The cpan documentation is not very explicit for this

谢谢!

PS:我愿意接受任何评论或建议:)

PS: I'm open to any remarks or suggestion :)

经过一些测试,我可以说命令已发送但被中断.我的测试是在远程服务器上启动另一个 perl 脚本.此脚本写入平面文件.在这个测试中,脚本被启动,文件被填满了一半.我的意思是,文件被粗暴地停在了中间.

After some test, i can say that the command is sent but is interrupted. My test was to start another perl script on the remote server. This script writes in a flat file. In this test, the script is started, the file is half-filled. I mean, the file is brutaly stopped in the middle.

另一方面,当我在$channel->exec()"之后执行sleep(10)"时,脚本成功结束.问题是,我不能写一个睡眠(10)"(我不知道它是否需要 9 或 11 秒(或更多,你明白我的意思)

In the other hand, when i performed a "sleep(10)" just after the "$channel->exec()", the script goes to the end successfully. Problem is, that I can't write a "sleep(10)" (i don't know if it will take 9 or 11 seconds (or more, you see my point)

推荐答案

您可以尝试使用 Net::SSH::Any 代替.

You can try using Net::SSH::Any instead.

它提供了更高级别且更易于使用的 API,并且可以使用 Net::SSH2 或 Net::OpenSSH 来处理 SSH 连接.

It provides a higher level and easier to use API and can use Net::SSH2 or Net::OpenSSH to handle the SSH connection.

例如:

use Net::SSH::Any;

my $ssh = Net::SSH::Any->new($host, user => $user, password => $password);
$ssh->error and die $ssh->error;

my $sftp = $ssh->sftp;
$sftp->put('local_path', 'remote_path');

my $output = $ssh->capture($cmd);
print "command $cmd output:\n$output\n\n";

$sftp->put('local_path1', 'remote_path1');

# no need to explicitly disconnect, connections will be closed when
# both $sftp and $ssh go out of scope.

请注意,我刚刚上传到 CPAN 的 0.03 版已添加了 SFTP 支持(通过 Net::SFTP::Foreign).

Note that SFTP support (via Net::SFTP::Foreign) has been added on version 0.03 that I have just uploaded to CPAN.

这篇关于[Perl][net::ssh2] 如何在执行远程命令时保持 ssh 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆