【问题标题】:How to store ip address into variable and pass it to shell command?如何将ip地址存储到变量中并将其传递给shell命令?
【发布时间】:2016-08-04 16:13:01
【问题描述】:
$ip=$_SERVER['REMOTE_ADDR'];
$mac=shell_exec('arp-a'.'$ip')
echo $mac;

我想获取机器的 ip 地址并将其传递给 shell 命令,如果我手动提供 ipaddress 但通过变量它不起作用。如果手动提供 ip,则运行良好,例如:$ mac=shell_exec('arp -a 192.168.0.32');

【问题讨论】:

  • 你是如何执行脚本的?
  • 你期待什么结果?
  • 你在哪个平台上?

标签: php html shell


【解决方案1】:

你忘记了空格:

$mac=shell_exec('arp-a'.'$ip')
                    ^-^--here

所以你正在执行的命令实际上是

arp-a127.0.0.1

而不是

arp -a 127.0.0.1

请注意,' 不会插入变量,因此您将文字 $ip 字符传递给 shell。你根本不需要引号:

$mac = shell_exec('arp -a ' . $ip);

【讨论】:

  • 那么不要使用shell_exec()。使用exec() 并捕获输出和退出状态以查看发生了什么。
【解决方案2】:

您的 $ip 用引号引起来,因此您将实际的 "$ip" 文字传递给 shell。

试试这个

$mac=shell_exec('arp-a '.$ip)

【讨论】:

    猜你喜欢
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 2015-08-05
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多