View unanswered posts | View active topics It is currently Mon Aug 21, 2017 5:25 am



Reply to topic  [ 4 posts ] 
 connecting to MySQL through ssh2_tunnel 
Author Message
Junior Member

Joined: Thu Jul 24, 2008 4:35 pm
Posts: 1
Post connecting to MySQL through ssh2_tunnel
I'm trying to connect one linux server to another linux server's mysql db using the PHP function ssh_tunnel().

Background:
I can access the linux server's mysql db on my Mac by typing in the command line:

Code:
ssh username@serverIP -L 3307:192.168.0.100:3307


Then I use CocoaMySQL to access the DB.

Current:
I want to do about the same thing with PHP to connect one linux server to the other linux server's mysql db. Here's what I've tried:
[php]
$connection = ssh2_connect('serverIP', 22);
if (ssh2_auth_password($connection, 'username', 'secret')) {
echo "Authentication Successful!\n";
} else {
die('Authentication Failed...');
}
print"

";
if(function_exists("ssh2_tunnel"))
echo "success";
else
echo "failure";
print"

";
$tunnel = ssh2_tunnel($connection, '192.168.0.100', 3307);

$conn = mysql_connect('127.0.0.1', 'DBusername', 'DBpassword' ) or die (mysql_error());

[/php]

The ouput I get is:

[html]
Authentication Successful!

success

Access denied for user 'DBusername'@'localhost' (using password: YES)
[/html]

But If I change the username and password to the local mysql username and password, then I get access to the local mysql db but not the remote mysql db.

Any ideas how to connect to the remote server, through ssh, and use the mysql_connect() function?


Thu Jul 24, 2008 4:55 pm
Report this post
Profile
Junior Member

Joined: Tue Nov 10, 2009 2:15 pm
Posts: 2
Post 
ssh2_tunnel() returns a socket type resource or something similar.

Then if you want to forward your local ports through the tunnel, you must need to bind a socket to listen in your local client machine, and then write the incoming data through the tunnel using fwrite($tunnel,$data_to_send); or something similar.

I'm now trying to figure out how to send data through the tunnel, fwrite seems doesn't working for this propose, but, this is an approach.


Tue Nov 10, 2009 2:21 pm
Report this post
Profile
Junior Member

Joined: Tue Nov 10, 2009 2:15 pm
Posts: 2
Post 
This finally produces some results:

[php]
<?php

//ssh2_tunnel() example.
//forwarding a local client port to server port (only send)

// just run:

// $ netcat -l -k -p <REMOTEPORT>
// on server side, and replace define(REMOTEPORT,XX) with same number port that you've used on netcat's
// command line.

// Then, run this script and open your browser with http://localhost:
// you should see the plain http browser query on your remote SSH2 server terminal

// As in this example, just use:
// $ netcat -l -k -p 8081
// and type http://localhost:8888 on your browser

// Note: this script is only illustrative. Is not something to be used as is in the real world.


define(SSH2SERVER,"<your_server>");
define(SSH2SERVER_PORT,22);
define(SSH2USER,"<your_user>");
define(SSH2PASS,"<your_pass>");

define(REMOTEPORT,"8081");
define(LOCALPORT,"8888");

function stdout($string) {
echo($string);
ob_flush();
}

function tunnel_disconnect($reason, $message, $language) {
global $setbreak;

printf("Server disconnected with reason code [%d] and message: %s\n",
$reason, $message);
$setbreak=true;
}


function open_tunnel($host) {
global $setbreak;

$setbreak=false;
$methods = array(
'kex' => 'diffie-hellman-group1-sha1',
'client_to_server' => array(
'crypt' => 'blowfish-cbc,aes256-cbc,3des-cbc',
'comp' => 'none'),
'server_to_client' => array(
'crypt' => 'blowfish-cbc,aes256-cbc,aes192-cbc,aes128-cbc',
'comp' => 'none'));

$callbacks = array('disconnect' => 'tunnel_disconnect');

stdout(" --> Openning SSH2 connection ... ");
$conn = ssh2_connect($host, SSH2SERVER_PORT, $methods);

if (ssh2_auth_password($conn, SSH2USER, SSH2PASS)) {
stdout("Successful!\n");
$tunnel = ssh2_tunnel($conn, '127.0.0.1', REMOTEPORT);
if (is_resource($tunnel)) {
stdout(" --> Tunnel sucesfully established.\n");
$sock = socket_create_listen(LOCALPORT);
if(is_resource($sock)) {
stdout(" Local sockets opened sucesfully.\n");
socket_set_block($sock);
while(!$setbreak) {
if (($localsock = @socket_accept($sock)) !== false) {
stdout(" --> Client Connected\n");
while (is_resource($localsock)) {
$x = @socket_read($localsock, 4096);
if ($x !== false && strlen ($x)) {
stdout(" + Sending data [" . strlen($x) . "] bytes\n");
fwrite($tunnel,$x);
}
}
}
}
}
socket_close($sock);
} else
stdout(" Error openning tunnel!\n");
} else
stdout("Failed!\n");

}

while (true) {
open_tunnel(SSH2SERVER);
sleep(10);
}

?>
[/php]Bye!


Tue Nov 10, 2009 3:49 pm
Report this post
Profile
Junior Member

Joined: Wed Aug 31, 2011 4:38 am
Posts: 2
Post Re: connecting to MySQL through ssh2_tunnel
Hi, i have the same problem of GREg, it seems there was a change in HostGator MySql settings. I can still remotely connect to MySql servers fine, but NONE OF MY databases appear after I connect, only information_schema... i use my cpanel user access and it is the same.

what it is the trouble?


Thu Sep 01, 2011 12:41 am
Report this post
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.