我想知道如何从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 connectionn";
} else {
// try to authenticate with username root, password secretpassword
if(!ssh2_auth_password($con, "root", "password")) {
echo "fail: unable to authenticaten";
} 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 commandn";
} 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 ",但同样的事情...谢谢和最好的问候!
将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))) {