0

Actually this is continue of Some try catch issue with php question. I'm using tutorial to create class for editing crontab. I have a file with class:

<?php
  Class Ssh2_crontab_manager 
  {
    private $connection;
    private $path;
    private $handle;
    private $cron_file;
    function __construct($host=NULL, $port=NULL, $username=NULL, $password=NULL)
    {
      $path_length    = strrpos(__FILE__, "/");
      $this->path      = substr(__FILE__, 0, $path_length) . '/';
      $this->handle    = 'crontab.txt';
      $this->cron_file = "{$this->path}{$this->handle}";

      try
      {
        if ((is_null($host)) || (is_null($port)) || (is_null($username)) || (is_null($password))) 
          throw new Exception("Please specify the host, port, username and password!");
        /*$this->connection = @ssh2_connect($host, $port);
        if ( !$this->connection)
          throw new Exception("The SSH2 connection could not be established.");

        $authentication = @ssh2_auth_password($this->connection, $username, $password);
        if ( !$authentication) 
          throw new Exception("Could not authenticate '{$username}' using password: '{$password}'.");*/    
      }
      catch (Exception $e)
      {
        echo $e;
      }
    }    
  }
?>

And a file where I use it:

<?php
  include './lib/Ssh2_crontab_manager.php';
  $crontab = new Ssh2_crontab_manager('host', '22', 'user', 'pass');
  echo 'WORKS';
?>

And again it says WORKS now, but if I uncomment string it doesn't says nothing. Now I use code from the end of step, so I don't repeat mistake I get in previous question. libssh2 was installed, may be problem that it wasn't successfully installed? How can I check it.

Edit: from phpinfo() libSSH Version libssh2/1.4.2
UPDATE

<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
//include './lib/Ssh2_crontab_manager.php';
//$crontab = new Ssh2_crontab_manager('host', '22', 'user', 'pass');
echo "WORKS";
    $connection = ssh2_connect('host','22'); //new variable ...not needed this class
    /*if ( !$connection)
    { 
        echo "The SSH2 connection could not be established.";
    }
    else
    {
        echo"OK";
    }*/
    echo "OK?";
    $authentication = @ssh2_auth_password($connection, 'user', 'pass');
?>

Now I test it like this, code still stucks on ssh2_connect.

8
  • does php support ssh2? check it using phpinfo() ... Commented Nov 5, 2014 at 7:29
  • yep, found a row there libSSH Version libssh2/1.4.2 Commented Nov 5, 2014 at 7:32
  • remove the "@" before ssh2_connect() so that you can see if there is any warning. And give a a real host name instead of ('host', '22', ...) And use error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); on the top of script to see what is the issue shown ... Commented Nov 5, 2014 at 7:48
  • 1.In my app I use real host name, I've replaced it on host. user passw etc. 2. I removed @ and result is the same 3. Also added error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); at top of the class. Still no results Commented Nov 5, 2014 at 7:53
  • Seems the code get stuck on ssh2_connect OR ssh2_auth_password() ... can u just echo "" instead of throwing exception..with an else statement. And install SSH2 PECL extension ...OR Use API from phpseclib.sourceforge.net Commented Nov 5, 2014 at 8:18

1 Answer 1

1

Seems you are having timeout on your script. Check ssh connection PHP BUG here. Try to set a time limit to your script OR edit php.ini for max_execution_time.

You can use SSH API to make it work.

Sign up to request clarification or add additional context in comments.

3 Comments

gonna test your answer later.
I've decided to use API. But I'll mark your answer because you adviced it in your comments.
Thanks @SergeyScopin . i have added this to the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.