On 2012-01-17 11:46, Ben Morrow <> wrote:
> Try system("ssh -f -L... ... sleep 10") instead of open3. It's important
> with -f to use 'sleep 10' rather than -N, otherwise the ssh process will
> never exit. (It doesn't seem to be very easy to find its pid to kill it
> manually.)
open($fh, '-|', ...) returns the pid, so does fork. The following script
works for me, at least on linux:
#!/usr/bin/perl
use warnings;
use strict;
use IO::Socket::INET;
$| = 1;
print "opening tunnel ... ";
my $pid = open(my $fh, '-|',
'ssh', '-N', '', '-L', '10007:chronos.DOMAIN:7'
) or die;
print " done (pid=$pid)\n";
sleep 5;
system('lsof', '-i', ':10007');
sleep 5;
print "opening socket ... ";
my $sock = IO::Socket::INET->new(PeerHost => 'localhost',
PeerPort => 10007,
Proto => 'tcp');
print " done\n";
print "sending request ... ";
print $sock "test123\n";
print " done\n";
print "reading response ... ";
my $resp = <$sock>;
print " done (resp = $resp)\n";
print "closing socket ... ";
close($sock);
print " done\n";
sleep(5);
system('lsof', '-i', ':10007');
sleep(5);
print "closing tunnel ... ";
kill(15, $pid);
my $rc = waitpid($pid, 0);
print " done (rc = $rc)\n";
sleep(5);
system('lsof', '-i', ':10007');
__END__
hp
--
_ | Peter J. Holzer | Deprecating human carelessness and
|_|_) | Sysadmin WSR | ignorance has no successful track record.
| | |
|
__/ |
http://www.hjp.at/ | -- Bill Code on