swares has asked for the wisdom of the Perl Monks concerning the following question:
This code does not seem to return its data... I can see in the log that the ssh command does seem to be successful.{ binmode(STDOUT); # Required for this to work on MSWin32 my $cmd = shift; my $nick = shift; my $who = shift; my $where = shift; my $channel=shift; my $args=shift; my $msg=shift; my %hash = %$args; my $data; my $filter = POE::Filter::Reference->new(); my $debug="true"; # Code goes Below # use DBI; use Date::Manip; # You need to return $status for the bot log and # $result for the user. You don't need any Print commands. my ($search) = $msg =~ /^!$cmd (.+)/; $search = trim($search); my $date = UnixDate(ParseDate($search), '%m/%d/%y %I:%M:%S%p %Z (% +z)'); if ($debug){print "cmd[$cmd] nick[$nick] who[$who] where[$where] c +hannel[$channel] args[$args] search[$search] date[$date]\n"}; my $title = $public_handlers{$cmd}->{'args'}->{'title'}; my $database = $public_handlers{$cmd}->{'args'}->{'database'}; my $user = $public_handlers{$cmd}->{'args'}->{'user'}; my $password = $public_handlers{$cmd}->{'args'}->{'password'}; my $table = $public_handlers{$cmd}->{'args'}->{'table'}; my $select = $public_handlers{$cmd}->{'args'}->{'select'}; my $header = $public_handlers{$cmd}->{'args'}->{'header'}; my $input = $public_handlers{$cmd}->{'args'}->{'input'}; my $sqlfilter = $public_handlers{$cmd}->{'args'}->{'filter'}; my $datenow = `date +"%Y-%m-%d %H:%M:%S"`; chomp($datenow); $select =~ s/\$date_now/$datenow/g; if ($debug){print "title[$title] database[$database] user[$user] p +assword[$password] table[$table] select[$select] header[$header] inpu +t[$input] sqlfilter[$sqlfilter]\n"}; my $result = "Performing SQL Query for [$select]\n $title\n"; my $status = "Performing SQL Query for [$select]\n $title\n"; # return results my %result = ( task => $cmd, status => $status, reply_to => $nick, + result => $result ); my $output = $filter->put( [ \%result ] ); print @$output; # query db my @row; my $dbh = DBI->connect("DBI:mysql:$database:localhost:3306","$user +", "$password", { RaiseError => 1, AutoCommit => 1 }); my $sth = $dbh->prepare(qq!SELECT * FROM $table WHERE $select!); $sth->execute; my $x=0; while ((@row) = $sth->fetchrow_array) { my $line= join(" ", map {defined $_ ? $_ : ""} @row); $status = "[$x] $line\n"; $result = "[$x] $line\n"; # return results %result = ( task => $cmd, status => $status, reply_to => $nick +, result => $result ); $output = $filter->put( [ \%result ] ); print @$output; $x++; if ($x > 9){last}; } $sth->finish; $dbh->disconnect; $status = "Done.\n"; $result = "Done.\n"; # Code Goes Above # # return results %result = ( task => $cmd, status => $status, reply_to => $nick, re +sult => $result ); $output = $filter->put( [ \%result ] ); print @$output; }
Any ideas? Did I make some dumb error in the coding or do I need some POE filter? I'm just not seeing it. Sorry if the code is ugly... its a prototype. Thanks{ # forking plugin to run a command on a remote server via ssh # non interactive and buffered. output returns after command completes binmode(STDOUT); # Required for this to work on MSWin32 my $cmd = shift; my $nick = shift; my $who = shift; my $where = shift; my $channel=shift; my $args=shift; my $msg=shift; my %hash = %$args; my $data; my $filter = POE::Filter::Reference->new(); my $cmdargs=""; my ($plugin, $user, $pass, $host, $cmdx, @args)=split /\s+/, $msg; # print "nick[$nick] who[$who]\n"; # print "got ($plugin, $user, $pass, $host, $cmdx, @args)\n"; for my $arg (@args){ $cmdargs .= " " . $arg; } use Net::SSH::Perl; my $ssh = Net::SSH::Perl->new($host, options => [ "BatchMode yes", "RhostsAuthentication no", "Debug true" ]); $ssh->login($user, $pass); my ($stdout, $stderr, $exit) = $ssh->cmd($cmdx, $cmdargs); chomp($stdout);chomp($stderr); # remove (translate) non-printable chrs $exit =~ tr/\x80-\xFF//d; $stderr =~ tr/\x80-\xFF//d; $stdout =~ tr/\x80-\xFF//d; my $status = "exit[$exit] stderr[$stderr] stdout[$stdout]\n"; my $result = $status; # Code Goes Above # # return results print "task[$cmd] status[$status] reply_to[$nick] Result[$resu +lt]\n"; my %result = ( task => $cmd, status => $status, reply_to => $n +ick, result => $result ); my $output = $filter->put( [ \%result ] ); print @$output; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Data structures not returned via POE::Filter::Reference
by rcaputo (Chaplain) on Apr 01, 2008 at 18:16 UTC | |
by swares (Monk) on Apr 08, 2008 at 16:21 UTC |