Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!not-for-mail From: "M. Strobel" Newsgroups: comp.lang.php Subject: Re: How to etablish an SSH2 tunnel with php ? Date: Sun, 01 Apr 2012 00:56:24 +0200 Lines: 106 Message-ID: <9tpgcoF32fU1@mid.uni-berlin.de> References: <4f773369$0$6487$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de 46gPZ8MhB4OpxyqSrm1RDgnt4eiMmvDL9ObTiyODaCbP+6Ho0= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; de; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20 In-Reply-To: Xref: csiph.com comp.lang.php:5295 Am 31.03.2012 19:35, schrieb Mr. B-o-B: > Une B=C3=A9vue cried from the depths of the abyss... >=20 >> The purpose is to query a remote PostgreSQL database via an ssh tunnel= =2E >> >> If i do the tunnel "by hand" from terminal using : >> $ ssh -L 3333:localhost:5432 yt@iMac >> >> then, i can query a remote database : >> $host=3D"localhost"; >> $port=3D3333; >> $username=3D'yt'; >> $password=3D'topsecret'; >> $db =3D new PDO("pgsql:dbname=3D$dbname;host=3D$host;port=3D$port", $= username, $password ); >> $ret=3D$db->query('SELECT * FROM categories;'); >> if($ret){ >> while($row=3D$ret->fetch()){ >> print_r($row); >> } >> }else{ >> echo 'Error'; >> } >> >> i've installes libssh2 for PHP on this computer, here is part of my in= fo.php : >> >> SSH2 support enabled >> extension version 0.11.2 >> libssh2 version 1.2.6 >> banner SSH-2.0-libssh2_1.2.6 >> remote forwarding enabled >> hostbased auth enabled >> polling support enabled >> publickey subsystem enabled >> >> however, even if i can "connect", authentification fail, either using = password or >> keys... >> >> the code used : >> function connect_to($machine) >> { >> $connection=3D@ssh2_connect($machine, 22, array("hostkey"=3D>"ssh-dsa= ")); >> if(!$connection){ >> echo "No connection.
\n"; >> return false; >> } else { >> echo "Connection =C3=A9tablie.
\n"; >> } >> >> $fingerprint=3D@ssh2_fingerprint($connection, SSH2_FINGERPRINT_MD5 | >> SSH2_FINGERPRINT_HEX); >> echo "\$fingerprint =3D $fingerprint
\n"; >> >> /* Utilisation de public/private key */ >> if(@ssh2_auth_pubkey_file($connection, "yt", >> '/home/yt/.ssh/id_dsa.pub', '/home/yt/.ssh/id_dsa', >> 'my -valid- passphrase')){ >> echo "Authentification r=C3=A9ussie.
\n"; >> return array($connection,$fingerprint); >> } else { >> echo "=C3=89chec de l'authentification.
\n"; >> return false; >> } >> } >> >> notice i get "Connection =C3=A9tablie" and also the fingerprint. >> >> if after the print out of fingerprint i try a command i get nothing af= ter an amout >> of time but without error : >> $stdout_stream=3D@ssh2_exec($connection, 'ls -al'); >> >=20 > I don't know the answer here, but I have several setups using remote db= 's & I use > stunnel (SSL) instead of SSH. >=20 > http://www.stunnel.org >=20 > Assuming you have static IP's on both sides it works well. End result = is the same. >=20 > Good Luck! >=20 > Mr. B-o-b stunnel is a good solution when it is difficult to setup SSL/TLS directly= in/for a service. This is not the case with Postgresql, you get remote access and ssl capab= ility by giving it a certificate, and putting your IP into pg_hba.conf. Very easy = to configure IMO. I use it a lot, and the good thing is the client libraries support i= t out of the box. So I would question the need to use a ssh tunnel. /Str.