dongqi9125 2014-08-27 13:55
浏览 62

插入启用密码ssh2 PHP

When I execute the command enable with ssh2_exec(), I can't load the page because it is waiting for the password.
I tried to do ssh2_exec(connection,'password') and the problem persists.
My question is how to put the enable password ? here is my code :

 <?php

if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist");
// log in at server1.example.com on port 22

if(!($con = ssh2_connect("9.0.0.1", 22))){
    echo "fail: unable to establish connection
";
} else { 
    // try to authenticate with username root, password secretpassword
    if(!ssh2_auth_password($con, "mehdi", "123")) {
        echo "fail: unable to authenticate
";
    } else {
        // allright, we're in!
        echo "okay: logged in...
";

        // execute a command
        if (!($stream = ssh2_exec($con, 'enable'))) {
            echo "fail: unable to execute command
";
        } else {  
            // collect returning data from command
            stream_set_blocking($stream, true);
            ssh2_exec($con, 'PASSWORD');
            $data = "";

            while ($buf = fread($stream,4096)) {

                $data .=PHP_EOL. date("Y-m-d H:i:s ").$buf;
                $data .= PHP_EOL."------------------------------------------------------------------------------------------------------------------------
";
            }
            echo $data;
            $fh = fopen("log".date("Y-m-d").".txt", 'a+') or die("can't open file");
            fwrite($fh, $data);
            fclose($fh);
            fclose($stream);
        }
    }
}
?>

ps: i am trying to configure a Cisco router

  • 写回答

1条回答 默认 最新

  • du4629 2014-08-29 04:06
    关注

    what about this (with phpseclib)?

    <?php
    include('Net/SSH2.php');
    
    $ssh = new Net_SSH2('9.0.0.1');
    $ssh->login('mehdi', '123');
    
    $ssh->setTimeout(3);
    
    $ssh->enablePTY();
    $ssh->exec('enable');
    $ssh->read('password:'); // or whatever the password prompt is
    $ssh->write("password\m"); // or maybe without the 
    
    echo $ssh->read();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分