php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60436 ssh2_connect(): Unable to connect to 172.18.xx.xxx on port 22
Submitted: 2011-12-03 14:17 UTC Modified: 2012-06-14 21:47 UTC
Votes:2
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
 [2011-12-03 14:17 UTC] shiv dot modi4 at gmail dot com
Description:
------------
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 php_ssh2.so. 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 172.16.xxx.xx] PHP Warning:  ssh2_connect(): Unable to connect to 172.18.xx.xxx on port 22 in /var/www/html/connection.php on line 9
[Sat Dec 03 19:30:35 2011] [error] [client 172.16.xxx.xx] PHP Warning:  ssh2_connect(): Unable to connect to 172.18.xx.xxx 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("172.18.xx.xxx", 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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 172.18.xx.xxx
 [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:

http://phpseclib.sourceforge.net/documentation/net.html#net_ssh_debug
 [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.

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

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] langemeijer@php.net
-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
libssh2/1.4.3
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Nov 27 20:01:24 2020 UTC