Using Net::SSH2, I've debugged the reason why your remote process doesn't return control quick enough, you need to daemonize your remote child. See perlipc on how to dissociate a child process from its parent.
The following code uses Net::SSH2, because that is what I have available but should be easily adapted to any other SSH client. The interesting
part is the oneliner string that is basically the daemonize subroutine. In my test case, the remote process simply sleeps three seconds and then creates a file:
#!perl -w
use strict;
use Net::SSH2;
my $remote_cmd = q{perl -MPOSIX=setsid -wle 'close STDIN;open STDOUT,"
+>","/dev/null";$SIG{HUP}="ignore";fork and exit;setsid;sleep 3; open
+my $fh, ">", "/tmp/test.txt" or die $!'};
my $ssh = Net::SSH2->new();
$ssh->connect('mychat.dyn.datenzoo.de');
$ssh->auth(username => 'corion', password => 'rGj3HVR4');
sub run($) {
my $ch = $ssh->channel;
$ch->ext_data('merge');
my $cmd = shift;
print "remote>$cmd\n";
$ch->exec($cmd)
or die $ssh->error;
my ($out,$err) = ('','');
$ch->send_eof;
while(!$ch->eof) {
if (my ($o, $e) = $ch->read2) {
$out .= $o;
$err .= $e;
}
#else {
# $ssh->die_with_error;
#}
};
print $out;
print $err;
};
run 'rm /tmp/test.txt';
run 'ls -altr /tmp/test.txt';
run $remote_cmd;
sleep 1;
run 'ls -altr /tmp/test.txt';
sleep 1;
run 'ls -altr /tmp/test.txt';
sleep 1;
run 'ls -altr /tmp/test.txt';
The output is as expected, after roughly three seconds, the file gets created by the remote process:
C:\Users\Corion\Projekte>perl -w tmp.pl
remote>rm /tmp/test.txt
remote>ls -altr /tmp/test.txt
ls: File not found: /tmp/test.txt
remote>perl -MPOSIX=setsid -wle 'close STDIN;open STDOUT,">","/dev/nul
+l";$SIG{HUP}="ignore";fork and exit;setsid;sleep 3; open my $fh, ">",
+ "/tmp/test.txt" or die $!'
Filehandle STDIN reopened as STDOUT only for output at -e line 1.
remote>ls -altr /tmp/test.txt
ls: File not found: /tmp/test.txt
remote>ls -altr /tmp/test.txt
ls: File not found: /tmp/test.txt
remote>ls -altr /tmp/test.txt
-rw-r--r-- 1 corion corion 0 Jun 16 18:49 /tmp/test.txt
|