Created
July 13, 2017 16:10
-
-
Save petenelson/f4bb800c2646043e391ced664aaa67d1 to your computer and use it in GitHub Desktop.
WordPress: MySQL PDO command via SSH tunnel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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