从反引号执行的PHP问题

时间:2011-03-10 17:10:18

标签: php exec

我有以下PHP代码:

 for($i=1; $i <= 25; $i++){
        $ifoids = $ifoids." ifPhysAddress.$i "."ifType.$i "."ifSpeed.$i "."ifDescr.$i "."ifOperStatus.$i";
if($i%12==0){
                echo $idoids;
        $rtable=$rtable.`snmpget -Os -Ov -Oe -Oq -Ot -v 1 -c public $ip $ifoids`;
        $ifoids="";
        echo $i.": ".$rtable."</br>";       
}}

第二次尝试执行snmpget命令时,$ifoid变量发送的值与第一次相同。虽然命令之前的print()会打印正确的值。

2 个答案:

答案 0 :(得分:1)

如果运行附带的示例,您应该能够看到$ ifoids被清除。

for($i=1; $i <= 25; $i++) {
        $ifoids = $ifoids." ifPhysAddress.$i "."ifType.$i "."ifSpeed.$i "."ifDescr.$i "."ifOperStatus.$i";

        if($i%12==0) {
            $rtable = "snmpget -Os -Ov -Oe -Oq -Ot -v 1 -c public $ip $ifoids";
            $ifoids="";
            echo $i.": ".$rtable."</br>";
        }
}

示例输出:

注意:为了清晰起见,添加了换行符

12: snmpget -Os -Ov -Oe -Oq -Ot -v 1 -c public
ifPhysAddress.1 ifType.1 ifSpeed.1 ifDescr.1 ifOperStatus.1
ifPhysAddress.2 ifType.2 ifSpeed.2 ifDescr.2 ifOperStatus.2
ifPhysAddress.3 ifType.3 ifSpeed.3 ifDescr.3 ifOperStatus.3
ifPhysAddress.4 ifType.4 ifSpeed.4 ifDescr.4 ifOperStatus.4
ifPhysAddress.5 ifType.5 ifSpeed.5 ifDescr.5 ifOperStatus.5
ifPhysAddress.6 ifType.6 ifSpeed.6 ifDescr.6 ifOperStatus.6
ifPhysAddress.7 ifType.7 ifSpeed.7 ifDescr.7 ifOperStatus.7
ifPhysAddress.8 ifType.8 ifSpeed.8 ifDescr.8 ifOperStatus.8
ifPhysAddress.9 ifType.9 ifSpeed.9 ifDescr.9 ifOperStatus.9
ifPhysAddress.10 ifType.10 ifSpeed.10 ifDescr.10 ifOperStatus.10
ifPhysAddress.11 ifType.11 ifSpeed.11 ifDescr.11 ifOperStatus.11
ifPhysAddress.12 ifType.12 ifSpeed.12 ifDescr.12 ifOperStatus.12</br>

24: snmpget -Os -Ov -Oe -Oq -Ot -v 1 -c public
ifPhysAddress.13 ifType.13 ifSpeed.13 ifDescr.13 ifOperStatus.13
ifPhysAddress.14 ifType.14 ifSpeed.14 ifDescr.14 ifOperStatus.14
ifPhysAddress.15 ifType.15 ifSpeed.15 ifDescr.15 ifOperStatus.15
ifPhysAddress.16 ifType.16 ifSpeed.16 ifDescr.16 ifOperStatus.16
ifPhysAddress.17 ifType.17 ifSpeed.17 ifDescr.17 ifOperStatus.17
ifPhysAddress.18 ifType.18 ifSpeed.18 ifDescr.18 ifOperStatus.18
ifPhysAddress.19 ifType.19 ifSpeed.19 ifDescr.19 ifOperStatus.19
ifPhysAddress.20 ifType.20 ifSpeed.20 ifDescr.20 ifOperStatus.20
ifPhysAddress.21 ifType.21 ifSpeed.21 ifDescr.21 ifOperStatus.21
ifPhysAddress.22 ifType.22 ifSpeed.22 ifDescr.22 ifOperStatus.22
ifPhysAddress.23 ifType.23 ifSpeed.23 ifDescr.23 ifOperStatus.23
ifPhysAddress.24 ifType.24 ifSpeed.24 ifDescr.24 ifOperStatus.24</br>

答案 1 :(得分:0)

这是正确的代码:

for($i=1; $i <= 25; $i++){
        $ifoids = $ifoids." ifPhysAddress.$i "."ifType.$i "."ifSpeed.$i "."ifDescr.$i "."ifOperStatus.$i";
        if($i%12==0){
            $rtable="snmpget -Os -Ov -Oe -Oq -Ot -v 1 -c public $ip $ifoids";
            $ifoids="";
            echo $i.": ".$rtable."</br>";       
        }
}

它已被正确重置,但因为你自己连接$ rtable它看起来不起作用