2015-05-13 108 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

谢谢德文:) –