--- alx-src/branches/alx-web-060/include/common-functions.php 2011/11/23 16:31:54 3342 +++ alx-src/branches/alx-web-060/include/common-functions.php 2012/02/01 10:48:54 3343 @@ -6,40 +6,53 @@ { global $privkey_file; global $pubkey_file; + global $socket_timeout; if (!function_exists("ssh2_connect")) die("sshdo(): function ssh2_connect doesn't exist. You must install the ssh2 module from pecl library!"); - if(!($con = ssh2_connect($ip, "22"))) + // check if port exists + $con = fsockopen( $ip, "22", $errono, $errstr, $socket_timeout); + if (!$con) { - echo "sshdo(): failure, could not connect\n"; + echo "sshdo(): socket failure,
could not connect to port 22
"; } else { - if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file)) + // first off all close our open socket + fclose($con); + + if(!($con = ssh2_connect($ip, "22"))) { - echo "sshdo(): failure, could not login\n"; + echo "sshdo(): failure, could not connect
"; } else { - //echo "logged in\n"; - - if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?"))) + if(!ssh2_auth_pubkey_file($con, $user, $pubkey_file, $privkey_file)) { - echo "sshdo(): failure, could not execute command '".$cmd."'\n"; + echo "sshdo(): failure, could not login
"; } else { - // collect stream data - stream_set_blocking($stream, true); - $data = ""; - while ($buf = fread($stream,4096)) + //echo "logged in\n"; + + if(!($stream = ssh2_exec($con, "source /etc/profile; ".$cmd."; echo '__RETVAL__:'$?"))) + { + echo "sshdo(): failure, could not execute command '".$cmd."'
"; + } + else { - $data .= $buf; - //echo "buf=" .$buf."\n"; + // collect stream data + stream_set_blocking($stream, true); + $data = ""; + while ($buf = fread($stream,4096)) + { + $data .= $buf; + //echo "buf=" .$buf."\n"; + } + fclose($stream); + # get the last element of data as retval + $retval = explode('__RETVAL__:', trim($data)); } - fclose($stream); - # get the last element of data as retval - $retval = explode('__RETVAL__:', trim($data)); } } }