2014-08-27 100 views
0

当我使用ssh2_exec()执行命令enable时,我无法加载页面,因为它正在等待密码。
我试图做ssh2_exec(connection,'password')问题依然存在。
我的问题是如何把启用密码? 这里是我的代码:插入启用密码ssh2 PHP

<?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\n"; 
} else { 
    // try to authenticate with username root, password secretpassword 
    if(!ssh2_auth_password($con, "mehdi", "123")) { 
     echo "fail: unable to authenticate\n"; 
    } else { 
     // allright, we're in! 
     echo "okay: logged in...\n"; 

     // execute a command 
     if (!($stream = ssh2_exec($con, 'enable'))) { 
      echo "fail: unable to execute command\n"; 
     } 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."------------------------------------------------------------------------------------------------------------------------\n"; 
      } 
      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:我试图配置Cisco路由器

+1

您正在寻找['ssh2_auth_password();'(http://de2.php.net/manual/en/function.ssh2-auth-password.php) – DanFromGermany 2014-08-27 14:04:54

+0

没有,当我执行命令“enable”进入特权模式 – Lizinh 2014-08-27 14:20:14

+0

没有命令'enable'。请链接到官方文档,通过'enable'显示您的意思,并在问题中提供一些代码。没有这些信息,没有人能帮助你。 – DanFromGermany 2014-08-27 14:25:01

回答

1

你看这个(与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 \n 
echo $ssh->read(); 
+0

我收到这些错误: 注意:无法打开C:\ wamp \ www \ CBI \ Net \ SSH2.php中的通道2995行 注意:无法在C:\ wamp \ www \ CBI \ Net \ SSH2.php 2529行 注意:在登录前不允许在2482行的C:\ wamp \ www \ CBI \ Net \ SSH2.php中执行操作 – Lizinh 2014-08-30 14:01:21

+0

用户名/密码可能不是正确。你可以用'if(!$ ssh-> login('mehdi','123'))exit('bad login')替换'$ ssh-> login';'如果你仍然得到错误,一个“糟糕的登录”消息? – neubert 2014-09-01 15:49:00