in reply to How do you use Net::OpenSSH to query mysql database via ssh tunnel
At the moment I don't see a way to hand an existing socket object like the one returned from $ssh->open_tunnel to DBD::mysql.
It seems like what you want is the equivalent of ssh -L, but looking into the documentation of Net::OpenSSH, I'm not sure that's supported, as it has this to say about tunnels:
Under the hood, in order to create a tunnel, a new ssh process is spawned with the option -W${address}:${port} (available from OpenSSH 5.4 and upwards) making it redirect its stdio streams to the remote given address. Unlike when ssh -L options is used to create tunnels, no TCP port is opened on the local machine at any time so this is a perfectly secure operation.
You might be able to DIY using the ssh_opts option. I'm not an expert on Net::OpenSSH so I can't say if there's a better way, but this worked for me (tested with a different server, not MySQL, but that shouldn't make a difference):
use Net::OpenSSH; #$Net::OpenSSH::debug |= 16; my $ssh = Net::OpenSSH->new($host); die $ssh->error if $ssh->error; my $pid = $ssh->spawn({ssh_opts=>'-L 127.0.0.1:12345:127.0.0.1:3306'}, + 'cat'); die $ssh->error if $ssh->error; # connect to remote MySQL via TCP at local 127.0.0.1:12345 sleep 10; # do your work here print "Ending...\n"; kill 'INT', $pid; waitpid ($pid, 0);
|
|---|