我想对我正在跟踪的几千台机器(我自己的机器)执行一些非阻塞 SSH ,我有一个 Dancer 应用程序并运行,我愿意使用 AnyEvent :: timer 异步执行 SSH 命令(每台机器都有自己的轮询间隔,我不想要一台机器等待另一个人完成其 SSH 工作)。
我想知道,在同步环境中异步操作的最佳方式是什么?
答案 0 :(得分:1)
从您的网络脚本中运行任何外部命令并不是一个好主意。 首先,如果您的外部呼叫因任何原因阻塞或崩溃,它将为用户创建糟糕的体验(即使该用户就是您)。 然后,作为Web用户运行外部命令可能会产生很多安全隐患 - 我认为您的Web用户很可能设置了无密码ssh,不是吗?如果有人在您的脚本中找出一些安全漏洞并设法将其用于ssh到您的服务器中,该怎么办?
相反,您应该创建单独的服务或流程,使用ssh(或其他)定期轮询您的服务器状态,并将该扫描的结果保存到数据库中 - Postgres或MySQL。
然后,更改您的Dancer应用程序以显示从数据库收集的结果,而不是执行实时ssh请求。这样它将非常快速和安全。
答案 1 :(得分:1)
我不是一个好主意,但它是可能的。我有一个很大的Dancer应用程序来远程执行脚本,我正在使用fork和Net :: SSH2。我尝试使用线程,但有些模块不是线程安全的,所以我建议使用fork。
我的博客http://perlondancer.blogspot.mx/2014/04/executing-remote-commands-from-dancer.html中有一些评论,这个要点是下面的代码示例:https://gist.github.com/johandry/11197516
#!/usr/bin/env perl
use strict;
use warnings;
use Dancer;
use Net::SSH2;
sub execCommand ($$) {
my ( $ssh2, $cmd ) = @_;
my %args=(
timeout => 1_000, # polling timeout
bufsize => 10_240, # read buffer size when polling
);
$ssh2->blocking(1); #needed for ssh->channel
my $chan=$ssh2->channel(); # create SSH2 channel
if ($ssh2->error()) {
return (undef, undef, 100);
}
# exec $cmd (caveat: only one command line can be executed over this channel. No "ls -l;whoami" combo. Use ssh->shell instead.
unless ($chan->exec($cmd)) {
return (undef, undef, 500);
}
# defin polling context: will poll stdout (in) and stderr (ext)
my @poll = ( { handle => $chan, events => ['in','ext'] } );
my %std=(); # hash of strings. store stdout/stderr results
$ssh2->blocking( 0 ); # needed for channel->poll
while(!$chan->eof) { # there still something to read from channel
$ssh2->poll( $args{'timeout'}, [ @poll ] ); # if any event, it will be store into $poll;
my( $n, $buf ); # number of bytes read (n) into buffer (buf)
foreach my $poll ( @poll ) { # for each event
foreach my $ev ( qw( in ext ) ) { #for each stdout/stderr
next unless $poll->{revents}{$ev};
#there are something to read here, into $std{$ev} hash
if( $n = $chan->read( $buf, $args{'bufsize'}, $ev eq 'ext' ) ) { #got n byte into buf for stdout ($ev='in') or stderr ($ev='ext')
$std{$ev}.=$buf;
}
} #done foreach
}
}
$chan->wait_closed(); #not really needed but cleaner
my $exit_code=$chan->exit_status();
$chan->close(); #not really needed but cleaner
$ssh2->blocking(1); # set it back for sanity (future calls)
return ($std{'in'},$std{'ext'},$exit_code);
}
sub execute ($$$$) {
my ($ip, $username, $password, $cmd) = @_;
my $pid = fork();
if ($pid) {
# This is the parent (DANCER)
debug "Process started with PID $pid\n";
} elsif ( $pid == 0 ) {
# This is the child
my $ssh2 = Net::SSH2->new();
$ssh2->connect( $ip ) or debug("Cannot connect to $ip");
my $publicKeyFile = './id_rsa.pub'; # path(setting('appdir'), 'db', 'id_rsa.pub'); # I prefer to copy the public key in your app dir due to permissions issues
my $privateKeyFile = './id_rsa'; # path(setting('appdir'), 'db', 'id_rsa'); # I prefer to copy the private key in your app dir due to permissions issues
if ( $ssh2->auth_publickey( $username, $publicKeyFile, $privateKeyFile, $password ) ) {
my ($stdout, $stderr, $exitcode) = execCommand($ssh2, $cmd);
} else {
debug "Could not authenticate to $ip with $username";
}
$ssh2->disconnect();
} else {
debug "Could not fork: $!\n";
}
}
set logger => "console";
set log => "core";
set show_errors => 1;
get '/uptime/:ip' => sub {
my $username = "the username";
my $password = "the password";
execute(param('ip'), $username, $password, "uptime > /tmp/dancer_example.txt");
return 'uptime is running';
};
dance;
true;
答案 2 :(得分:0)
Net :: SSH2可以异步使用,但它非常错误并经常崩溃。忘记使用它在同一进程上并行运行数千(或几百)个连接。如果您按照@Johandry推荐的方式将它包装在新进程中可能没问题,但是您可以使用ssh
运行AnyEvent::Util::run_cmd
命令。
Net :: OpenSSH是另一个可以异步使用的Perl模块。将它集成到AnyEvent中应该不会太困难。