2015-05-13 54 views
-1

我想知道我怎麼可以得到PHP運行命令 這裏是我的代碼:

<?php 
$msg = $_GET['msg']; 
echo "$msg test"; 
if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist"); 
if(!($con = ssh2_connect("ip", 22))){ 
    echo "fail: unable to establish connection\n"; 
} else { 
    // try to authenticate with username root, password secretpassword 
    if(!ssh2_auth_password($con, "root", "password")) { 
     echo "fail: unable to authenticate\n"; 
    } else { 
     // allright, we're in! 

     echo "okay: logged in...\n"; 


     // execute a command 
     if (!($stream = ssh2_exec($con, 'wall echo $msg'))) { 

      echo "fail: unable to execute command\n"; 
     } else { 
      // collect returning data from command 
      stream_set_blocking($stream, true); 
      $data = ""; 
      while ($buf = fread($stream,4096)) { 
       $data .= $buf; 
      } 
      fclose($stream); 
     } 
    } 
} 
?> 

所以,它不會牆我在味精=這樣說。 ..它只是寫空白,但當我回聲$味精(如我在代碼頂部)它寫入通常,你們知道哪裏是問題?我已經嘗試過「echo $ msg」和「echo $ msh」,但是同樣的事情......感謝和最誠摯的問候!

回答

1

將GET變量傳遞給ssh2_exec()可能是一個巨大的安全問題。但無視這一點,單引號忽略變量 - 你需要雙引號。

換句話說,然而改變這一

if (!($stream = ssh2_exec($con, 'wall echo $msg'))) { 

這個

if (!($stream = ssh2_exec($con, "wall echo $msg"))) { 

,我懷疑你正在嘗試使用echo作爲PHP構建,並且你只有真正感興趣在$msg變量中。在這種情況下,你可以做

if (!($stream = ssh2_exec($con, "wall $msg"))) { 

if (!($stream = ssh2_exec($con, 'wall ' . $msg))) { 
+0

它的工作原理!非常感謝你^ _^ –

+1

@McFilip用於安全使用''牆'。 escapeshellarg($ msg)' – Devon

+0

謝謝德文:) –