|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60436 ssh2_connect(): Unable to connect to on port 22
Submitted: 2011-12-03 14:17 UTC Modified: 2012-06-14 21:47 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: shiv dot modi4 at gmail dot com Assigned: langemeijer (profile)
Status: Closed Package: ssh2 (PECL)
PHP Version: 5.3.9RC2 OS: Centos
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: shiv dot modi4 at gmail dot com
New email:
PHP Version: OS:


 [2011-12-03 14:17 UTC] shiv dot modi4 at gmail dot com
I am trying connect another remote server using ssh. PHP provides a number of functions to do that. When i try to connect server2(assume) using ssh2_connect function from my local system wamp server it works. But when i try the same from my server1 to connect to server2 then it's not working. Everything is installed on server1 -- openssh, libssh2 etc. I have also done with changes with php.ini file by adding extension Folowing is the script i am using. It's showing the any error.

unable to establish connection

But when i checked the apache error log file. it shows the following

[Sat Dec 03 19:30:35 2011] [error] [client] PHP Warning:  ssh2_connect(): Unable to connect to on port 22 in /var/www/html/connection.php on line 9
[Sat Dec 03 19:30:35 2011] [error] [client] PHP Warning:  ssh2_connect(): Unable to connect to in /var/www/html/connection.php on line 9

I am trying to find the problem from last few days. Please help!!!!!

Test script:
if (!function_exists("ssh2_connect")) 
		die("function ssh2_connect doesn't exist");
	if(!($con = ssh2_connect("", 22))){
		echo "fail: unable to establish connection\n";
	} else {
		if(!ssh2_auth_password($con, "admin", "admin")) { 
			echo "fail: unable to authenticate\n";
		} else {
			echo "okay: root logged in...\n";
			echo ssh2_exec($con, "dir");

Expected result:
okay: root logged in... and list of directories

Actual result:
fail: unable to establish connection


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-03 21:03 UTC] anon at anon dot anon
You probably forgot to open the port on the firewall.
 [2011-12-04 07:12 UTC] shiv dot modi4 at gmail dot com
Then why it's connecting when i use the following command from shell prompt?

#ssh -p 22
 [2011-12-10 02:28 UTC] zelnaga at gmail dot com
I would use phpseclib, a pure PHP SSH implementation, and see what the logs produced by that say.  More info:
 [2011-12-14 19:31 UTC] mike dot mackintosh at angrystatic dot com
I always use the below before attempting to execute a connection via ssh2_connect/etc.

  $fp = fsockopen($host, 22, $errno, $errstr, 5);
     throw new Exception('Port 22 Is Obstructed - '.$errstr);	
     return false;	
}catch(Exception $e){

What are your results?
 [2011-12-15 19:24 UTC] shiv dot modi4 at gmail dot com
Guys thanks for your response. I am able to solve it. Actually apache was not allowed to send packets using tcp.

Here is the solution. Run this command from server.

Allow HTTPD scripts and modules to connect to the network using any TCP port.
# setsebool -P httpd_can_network_connect 1
 [2012-06-14 21:47 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: langemeijer
 [2016-09-29 03:46 UTC] fernando dot correa at mandi dot com dot br
Thanks guys, this solution works to me, 5 year after. Very well

# setsebool -P httpd_can_network_connect 1
 [2019-08-07 23:59 UTC] katiersissons at gmail dot com
8 years later and this solved my problem. Wow! Thank you!!

Server version: Apache/2.4.6 (CentOS)
Server built:   Apr 24 2019 13:45:48
PHP Version 5.4.16
 [2021-01-24 18:40 UTC] jimplumb at gmail dot com
# setsebool -P httpd_can_network_connect 1

Does the trick in 2021!

Thank you.
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Sep 29 00:01:24 2023 UTC