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!