[英]PHP: How to get the IP address of a specific VLAN
Background: A page should display SNMP data from various devices in a mgmt vlan, to loop through all IP addresses in the network. 背景:页面应显示来自mgmt vlan中各种设备的SNMP数据,以循环访问网络中的所有IP地址。 I need to know the IP address/network, the interface is on. 我需要知道IP地址/网络,该接口已打开。
In Bash this works perfectly: 在Bash中,这很完美:
/sbin/ip addr show dev eth0.1100 | /usr/bin/awk '/global/ { $2=substr($2, 1, length($2)-7); print $2 }'
But in PHP I can't find a way to get the IP address/network of the mgmt vlan. 但是在PHP中,我找不到找到mgmt vlan的IP地址/网络的方法。 I tried: 我试过了:
$mgmtiface = shell_exec("/usr/bin/sudo /usr/bin/awk '$3 == 1100 {print $1}' /proc/net/vlan/config");
$mgmtnet = shell_exec("/sbin/ip addr show dev $mgmtiface | /usr/bin/awk '/global/ { $2=substr($2, 1, length($2)-7); print $2 }' ");
But PHP doesn't like pipe in shell_exec . 但是PHP不喜欢shell_exec中的管道 。
How do I solve this? 我该如何解决?
Interesting, defining the interface hardcoded works fine, but as soon as I put a variable with the interface in it, it won't work: 有趣的是,定义硬编码的接口可以很好地工作,但是一旦我在其中添加了带有接口的变量,它将无法正常工作:
$mgmtnet = shell_exec("/sbin/ip addr show dev eth0.1100 | /usr/bin/awk '/global/ { $2=substr($2, 1, length($2)-7); print $2 }' ");
shell_exec
returns the entire output, including the trailing newline. shell_exec
返回整个输出,包括结尾的换行符。 You need to trim that off before you substitute into the next command: 您需要先修剪掉它,然后替换为下一个命令:
$mgmtiface = teim(shell_exec("/usr/bin/sudo /usr/bin/awk '$3 == 1100 {print $1}' /proc/net/vlan/config"));
$mgmtnet = trim(shell_exec("/sbin/ip addr show dev $mgmtiface | /usr/bin/awk '/global/ { $2=substr($2, 1, length($2)-7); print $2 }' "));
If you know the command just returns one line of output, you can use exec
instead of shell_exec
. 如果您知道该命令仅返回一行输出,则可以使用exec
代替shell_exec
。 It returns the last line, without the newline, so you don't need to trim it. 它返回最后一行,没有换行符,因此您不需要修剪它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.