2014-08-27 95 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