【问题标题】:PHP SSH2 exec "$"PHP SSH2 执行“$”
【发布时间】:2015-07-25 03:30:52
【问题描述】:

我想知道如何在 PHP 中从 GET 运行命令... 这是我的代码:

<?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);
        }
    }
}
?>

所以,它不会影响我在 ?msg= 中所说的话...它只是写空白,但是当我回显 $msg(就像我在代码顶部所做的那样)时,它会正常写入,你们知道在哪里吗?问题?我已经尝试过 "echo $msg" 和 \"echo $msh\" 但同样的事情......谢谢和最好的问候!

【问题讨论】:

    标签: php ssh exec


    【解决方案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))) {
    

    【讨论】:

    • @McFilip 用于安全用途 'wall ' . escapeshellarg($msg)
    猜你喜欢
    • 2020-07-25
    • 1970-01-01
    • 2018-03-31
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 2019-08-01
    相关资源
    最近更新 更多