从 shell_exe('df -h') 获取特定列数据作为关联数组
我需要构建一个脚本来查询具有多个驱动器的远程存储服务器的可用空间。
我只需使用 SSH 登录,运行 df 并将输出放入变量中。看起来像这样:
Array
(
[0] => Filesystem 1K-blocks Used Available Use% Mounted on
[1] => /dev/sda1 59392320 14949240 41426064 27% /
[2] => /dev/sdd1 140592240 114503840 18946708 86% /home/overflow
[3] => /dev/sdf1 287826944 273844324 0 100% /home/node2
[4] => /dev/sde1 287826944 253278964 19927228 93% /home/node3
[5] => /dev/sdg1 287826944 4771768 268434424 2% /home/node4
[6] => /dev/sdh1 287826944 4329780 268876412 2% /home/node5
[7] => /dev/sdi1 488302976 439077756 24420864 95% /home/node1
[8] => /dev/sdh1 287826944 4329780 268876412 2% /home/mnode6
[9] => /dev/sdh1 287826944 4329780 268876412 2% /home/mnode7
[10] => tmpfs 3145728 0 3145728 0% /tmp
)
我需要提取每个“节点”或“mnode”有多少可用空间。所以也许一个很好的数组有这样的对:
mnode1 => 24000000
node1 => 24000000
node2 => 0
node3 => 20000000
I need to build a script that will query a remote storage server with multiple drives for their free space.
I simply login with SSH, run df
and throw the output into a variable. Which looks like this:
Array
(
[0] => Filesystem 1K-blocks Used Available Use% Mounted on
[1] => /dev/sda1 59392320 14949240 41426064 27% /
[2] => /dev/sdd1 140592240 114503840 18946708 86% /home/overflow
[3] => /dev/sdf1 287826944 273844324 0 100% /home/node2
[4] => /dev/sde1 287826944 253278964 19927228 93% /home/node3
[5] => /dev/sdg1 287826944 4771768 268434424 2% /home/node4
[6] => /dev/sdh1 287826944 4329780 268876412 2% /home/node5
[7] => /dev/sdi1 488302976 439077756 24420864 95% /home/node1
[8] => /dev/sdh1 287826944 4329780 268876412 2% /home/mnode6
[9] => /dev/sdh1 287826944 4329780 268876412 2% /home/mnode7
[10] => tmpfs 3145728 0 3145728 0% /tmp
)
I need to extract how much free space each "node" or "mnode" has. So perhaps a nice array that has pairs such as this:
mnode1 => 24000000
node1 => 24000000
node2 => 0
node3 => 20000000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
explode(' ', preg_replace('/ +/', ' ', $line))
获取所有值均已拆分的数组。Use
explode(' ', preg_replace('/ +/', ' ', $line))
to get an array with all values split up.要生成一个关联数组,其中键来自
Mounted On
值,值来自Available
值,您可以循环并解析shell_exec( 'df')
输出字符串(忽略第一行中的标头)并使用 sscanf() 将数据解析为所需的关联数组。%*s
将匹配非空白字符,但不会捕获返回的匹配。我将使用数组解构(在第一个元素之前访问第二个元素,以最大程度地减少声明的变量总数。代码:
输出:
我还发布了一个非常类似问题的答案:仅显示 shell_exec('df') 中磁盘使用数据的特定列
To generate an associative array where the keys are from
Mounted On
values and the values are fromAvailable
values, you can loop over and parse the lines of theshell_exec('df')
output string (ignoring the headers in the first line) and usesscanf()
to parse the data into the desired associative array.%*s
will match non-whitespace characters, but not capture the match for returning. I'll use array destructuring (accessing the 2nd element before the 1st element to minimize the total number of variables declared.Code:
Output:
I have also posted an answer to a very similar question: Display only specific columns of disk usage data from shell_exec('df')