in reply to Re: How do you use Net::OpenSSH to query mysql database via ssh tunnel
in thread How do you use Net::OpenSSH to query mysql database via ssh tunnel

This person claims to have a way to avoid the sleep command: Opening and closing an SSH tunnel in a shell script the smart way

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

  • Comment on Re^2: How do you use Net::OpenSSH to query mysql database via ssh tunnel
  • Download Code

Replies are listed 'Best First'.
Re^3: How do you use Net::OpenSSH to query mysql database via ssh tunnel
by haukex (Archbishop) on May 09, 2017 at 18:39 UTC
    the sleep command

    Sorry, I should have been more clear on that: As far as I can tell, the sleep is not necessary in my code, I was simply using it for testing as a placeholder instead of connecting to the DB and doing work. As far as I can tell, the tunnel should remain open until you kill the slave process. So in my code, in the place where I commented "connect to remote MySQL via TCP at local 127.0.0.1:12345 / do your work here", that's what you should do :-)

    The cat is a placeholder that I figured would just sit there and do nothing while the tunnel is being used, which indeed seems to be the case. Then again, you should probably listen to the module's author.

Re^3: How do you use Net::OpenSSH to query mysql database via ssh tunnel
by nysus (Parson) on May 09, 2017 at 15:12 UTC

    So following the advice on the link above and another page it references, I got this:

    my $pid = $ssh->spawn({ssh_opts=> '-fL 127.0.0.1:12345:127.0.0.1:3306' +}, 'sleep 10');

    The big advantage is no more sleep command (except on the remote machine but that doesn't delay anything). Nice.

    And, apparently, as long as you do a query within 10 seconds it will work and it will autoclose the tunnel after that.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks