2013-07-27 51 views
0

我有一個php腳本,我在其中使用了自動登錄和在遠程計算機上執行命令。這是我的PHP代碼看起來像,PHP預計不能正常工作

<?php 
    ini_set("expect.loguser", "Off"); 
    $stream = fopen("expect://ssh [email protected]", "r"); 
    $cases = array (
     array (0 => "*(yes/no)?", 1 => YESNO), 
     array (0 => "*d: ", 1 => PASSWORD), 
     array (0 => "*$ ", 1 => SHELL, EXP_EXACT) 
    ); 
    switch (expect_expectl ($stream, $cases)) { 
     case YESNO: 
      fwrite ($stream, "yes\n"); 
      break; 

     case PASSWORD: 
      fwrite ($stream, "myrootpassword\n"); 
      break; 

     case SHELL: 
      fwrite ($stream, "top -n 1\n"); 
      break; 

     default: 
      die ("Error was occurred while connecting to the remote host!\n"); 
    } 
    while ($line = fgets($stream)) { 
     print $line; 
    }   
    fclose ($stream); 
?> 

我知道我已經安裝了PHP的期望模塊也。

enter image description here

我已經安裝了期待我的Ubuntu機器還。

enter image description here

但我不知道這是爲什麼腳本不能正常工作。我遵循http://www.php.net/manual/en/book.expect.phphttp://php.net/manual/en/expect.examples-usage.php來創建我的php腳本文件。請告訴我,因爲我是PHP的初學者,我需要做些什麼修改才能在php中工作。

謝謝。

回答

0

首先,大多數服務器默認配置爲不允許通過ssh遠程root登錄。

其次,爲什麼不使用SSH密鑰文件來允許無密碼登錄?這比存儲密碼更安全。

  1. 設置帳戶(在這個例子的情況下,我們稱之爲「自動化」),其具有的自動化指揮無密碼sudo訪問(見sudoers的NOPASSWD標誌)
  2. 使用ssh-keygen配置無密碼從遠程系統訪問該帳戶
  3. 代替使用expect://運行命令的,只需運行直接命令:

    ssh [email protected] sudo theCommand