PHP SSH2执行程序"$" [英] PHP SSH2 exec "$"
问题描述
我想知道如何在PHP中从GET运行命令... 这是我的代码:
i am wondering how i can run command from GET in PHP... here is my code:
<?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 \",但是同样的事情……谢谢,也最好的问候!
So, it wont wall what i say in ?msg= ... it just write blank, but when i echo $msg (like i did it on the top of code) it writes normally, do you guys know where is problem? i already tried "echo $msg" and \"echo $msh\" but same thing... thanks and best regards!
推荐答案
将GET变量传递给ssh2_exec()
可能是一个巨大的安全问题.但不管怎么说,单引号会忽略变量-为此,您需要双引号.
Passing a GET variable to an ssh2_exec()
is probably a huge security issue. But disregarding that, single quotes ignore variables - you need double quotes for that.
换句话说:更改此
if (!($stream = ssh2_exec($con, 'wall echo $msg'))) {
对此
if (!($stream = ssh2_exec($con, "wall echo $msg"))) {
但是,我怀疑您正在尝试将echo
用作PHP构造,并且您只对$msg
变量真正感兴趣.在这种情况下,您可以做
However, I suspect that you're trying to use echo
as a PHP construct, and you're only really interested in the $msg
variable. In that case, you can just do
if (!($stream = ssh2_exec($con, "wall $msg"))) {
或
if (!($stream = ssh2_exec($con, 'wall ' . $msg))) {
这篇关于PHP SSH2执行程序"$"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!