S
S
Sergey Belyakov2019-09-17 16:02:09
Yii
Sergey Belyakov, 2019-09-17 16:02:09

How to check the connection in a machine in php local network?

Good afternoon! I have a function that connects to a remote machine on the local network via ssh and executes the command I need. And she works. But if the machine is not available (turned off or something else), the application fails.
Here is the function

public function actionCommand($host,$username,$password){
        $console = "usbsrv -list";
        $yii_ssh = new Yii2ssh();
        $auth['username'] = $username; // логин
        $auth['password'] = $password; // пароль
        $session = $yii_ssh->connect($host, $auth); // подключаемся


        if($session){
            $lin = $yii_ssh->run_ssh($console);
            $line = explode("\n", $lin);
        }
        else{
            $line = false;
//            $line = 'Сервер не доступен, попробуйте позже';
        }
        return $line;
    }

And here is the error:
PHP Warning – yii\base\ErrorException
fclose() expects parameter 1 to be resource, boolean given
↵
Caused by: yii\base\ErrorException

in C:\OSPanel\domains\localhost\rutoken.loc\vendor\phpseclib\phpseclib\phpseclib\Net\SSH2.php at line 1152

5d80c407d6386002630022.png
The application is written in yii2, I use the behnampro\yii2ssh\Yii2ssh library in it . It allows you to execute commands via ssh on remote machines. How to check the connection to a machine on the local network using php or Yii2? I googled, but the solutions that I found did not suit me

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2019-09-17
@grey_18_08

The library is written crookedly, does not check if the connection was successful.
You probably don't want to rewrite it.
You can wrap this block in a try-catch, catch this very exception and process it more correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question