dongyin8009 2013-08-05 09:04
浏览 37

phpseclib:如何给出IF条件来检查建立的连接与否

I have PHP script which uses ssh utility to connect to a remote Ubuntu machine for executing some commands there. The script is as given below.

<?php
    include('Net/SSH2.php');

    define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

    $ssh = new Net_SSH2('10.3.2.0');
    if (!$ssh->login('makesubdomain','abcdabcd')) {
        exit('Login Failed');
    }
    echo $ssh->exec('/usr/local/listdomain.backup/test/makedir.sh');
?>

Here my problem is, how can I give an if condition to check whether the connection failed or not. I am trying to modify the above code something like this, but I know this is very wrong.

<?php
    include('Net/SSH2.php');

    define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

    if(!$ssh = new Net_SSH2('10.3.2.0')) // Here how can I give condtion
    {
        echo "Connection failed";
    }else{
         if (!$ssh->login('abcd','abcdabcd')) {
             exit('Login Failed');
         }
         echo $ssh->exec('./test.sh');
    }
?>

Please help me as I am very beginner in php. Thanks

  • 写回答

1条回答 默认 最新

  • dsbj66959 2013-08-05 09:33
    关注

    The answer you're looking for is:

    It's already all there in the example you've given, no need to modify the code.

    $ssh->login() does exactly what you want: It tries to connect AND login to the server you've given it, and it will return false in case that failed.

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)