Skip to content

Instantly share code, notes, and snippets.

@petenelson
Created July 13, 2017 16:10
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save petenelson/f4bb800c2646043e391ced664aaa67d1 to your computer and use it in GitHub Desktop.
Save petenelson/f4bb800c2646043e391ced664aaa67d1 to your computer and use it in GitHub Desktop.
WordPress: MySQL PDO command via SSH tunnel
<?php
/**
* Plugin Name: SSH Remote Test
*/
add_action( 'admin_init', 'ssh_remote_test', 1 );
function ssh_remote_test() {
// this is VERY basic demonstration code for running a remote MySQL command via SSH tunnel.
// Also relevant, https://gist.github.com/scy/6781836
$options = array(
'hostname' => '162.243.109.231',
'username' => 'pete',
'private_key' => '/srv/www/keys/scrubadump/id_rsa',
);
shell_exec( "ssh -i {$options['private_key']} -o StrictHostKeyChecking=no -fN -L 3307:127.0.0.1:3306 {$options['username']}@{$options['hostname']}" );
$dbh = new \PDO('mysql:host=127.0.0.1;port=3307;dbname=petenelson_io', 'petenelson_io', 'PASSWORD' );
$sth = $dbh->prepare("SELECT * from wp_users");
$sth->execute();
$result = $sth->fetchAll();
echo '<pre>';
var_dump( $result );
die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment