Guest User

Untitled

a guest
Jul 25th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. <?php
  2. $ssh_host = "1.2.3.4";
  3. $ssh_user = "my_ssh_username";
  4. $ssh_tunnel_local_port = "3307";
  5. $ssh_tunnel_remote_host = "127.0.0.1"; # DB Server IP on the remote network if not local to the remote system
  6. $ssh_tunnel_remote_port = "3306";
  7. $local_mysql_host = "127.0.0.1";
  8. $mysql_user = "my_mysql_username";
  9. $mysql_pass = "my_secret_password";
  10. $mysql_db_name = "my_database_name";
  11. $mysql_charset = 'utf8';
  12.  
  13.  
  14.  
  15. // Create background process for ssh tunnel and capture process id
  16. $ssh_opt = "-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o LogLevel=quiet -o ExitOnForwardFailure=yes";
  17. $ssh_command = "ssh $ssh_opt -N -L $ssh_tunnel_local_port:$ssh_tunnel_remote_host:$ssh_tunnel_remote_port $ssh_user@$ssh_host";
  18. $pid = shell_exec($ssh_command . " > /dev/null 2>&1 & echo $!");
  19.  
  20.  
  21.  
  22. // Wait for tunnel to establish before attemtping to initiate a MySQL connection
  23. echo "Waiting for tunnel to open...\n";
  24. date_default_timezone_set("UTC");
  25. $start_time = time();
  26. $connected = false;
  27. while ($connected === false && time() < $start_time+60) {
  28. // Test if port is able to be opened
  29. $fp = fsockopen($local_mysql_host, $ssh_tunnel_local_port, $errno, $errstr, 10);
  30. if (!$fp) {
  31. echo "$errstr ($errno)\n";
  32. echo "retrying...\n";
  33. } else {
  34. $connected = true;
  35. echo "ssh tunnel established\n";
  36. fclose($fp);
  37. }
  38. sleep(1); # Wait for ssh tunnel creation
  39. }
  40.  
  41.  
  42.  
  43. // Establish MySQL PDO connection and query available databases
  44. $dsn = "mysql:host=$local_mysql_host;port=$ssh_tunnel_local_port;dbname=$mysql_db_name;charset=$mysql_charset";
  45. $opt = [
  46. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  47. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  48. PDO::ATTR_EMULATE_PREPARES => false,
  49. ];
  50. try {
  51. echo "dsn: $dsn\n";
  52. echo "mysql_user: $mysql_user\n";
  53. echo "mysql_pass: $mysql_pass\n";
  54. $pdo = new PDO($dsn, $mysql_user, $mysql_pass, $opt);
  55.  
  56. $stmt = $pdo->prepare("SHOW DATABASES");
  57. $stmt->execute();
  58. var_dump($stmt->fetchAll());
  59. } catch (PDOException $e) {
  60. echo 'Connection failed: ' . $e->getMessage() . "\n";
  61. }
  62.  
  63.  
  64.  
  65. // Close SSH tunnel using the process id captured earlier
  66. echo "Checking connection for termination\n";
  67. try {
  68. // Check to see if the process is still running
  69. $result = shell_exec(sprintf('ps %d', $pid));
  70. if (count(preg_split("/\n/", $result)) > 2) {
  71. echo "pid: $pid\n";
  72. echo "$result\n";
  73. echo "ssh connection is still running, attemtping to close connection.\n";
  74. shell_exec("kill $pid");
  75. } else {
  76. echo "ssh tunnel was already closed.\n";
  77. }
  78. } catch(Exception $e) {
  79. echo 'Something bad happened: ' . $e->getMessage() . "\n";
  80. }
Add Comment
Please, Sign In to add comment