从输出构建数组
我正在尝试创建一个脚本来获取系统命令的输出,并且我想将数据组织到一个数组中。
该脚本用于发现驻留在我拥有的本地 ESXi 服务器上的虚拟机。我使用 plink.exe 将命令发送到服务器,然后它返回如下所示的虚拟机列表。
Vmid Name File Guest OS Version Annotation 128 NS01 [datastore2] NS01/NS01.vmx ubuntu64Guest vmx-07 144 NS02 [datastore2] NS02/NS02.vmx ubuntu64Guest vmx-07 208 MX01 [datastore2] MX01/MX01.vmx ubuntu64Guest vmx-07 224 SQL01 [datastore2] SQL01/SQL01.vmx ubuntu64Guest vmx-07 240 WS01 [datastore2] WS01/WS01.vmx ubuntu64Guest vmx-07
我将如何利用它并用它制作一个数组?唯一真正重要的列是 VMID、名称、文件
我用来获取输出的命令是这样的。
# Parse ESX\ESXi server for virtual machines that reside on it
system ("$plink \-batch \-pw $esx_password $esx_user\@$esx_host vim-cmdvmsvc/getallvms\n");
任何见解都会很棒。
I am trying to create a script that will take output of a system command and I want to organize the data into an array.
The script is to discover VMs residing on a local ESXi server I have. I am using plink.exe to send the command to the server and then it returns a list of VMs that looks like this.
Vmid Name File Guest OS Version Annotation 128 NS01 [datastore2] NS01/NS01.vmx ubuntu64Guest vmx-07 144 NS02 [datastore2] NS02/NS02.vmx ubuntu64Guest vmx-07 208 MX01 [datastore2] MX01/MX01.vmx ubuntu64Guest vmx-07 224 SQL01 [datastore2] SQL01/SQL01.vmx ubuntu64Guest vmx-07 240 WS01 [datastore2] WS01/WS01.vmx ubuntu64Guest vmx-07
How would I take this and make an array out of it? The only columns that really matter are VMID, Name, File
The command I am using to get the output is this.
# Parse ESX\ESXi server for virtual machines that reside on it
system ("$plink \-batch \-pw $esx_password $esx_user\@$esx_host vim-cmdvmsvc/getallvms\n");
Any insight would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,如果您想解析输出,请使用反引号而不是
系统
。system
将仅使用您的标准输出。不过,不确定为什么要转义破折号...
一旦获得输出,您可以执行以下操作(请注意,我利用了固定长度字段的外观):
Yes, use backquotes not
system
if you want to parse the output.system
will just use your standard out.Not sure why you were escaping the dashes, though...
Once you have the output, you can do the following (note that I leveraged the fixed-length fields look):
如果没有数据列包含空格,您可以轻松地将输出
split(/\n/)
分成几行,迭代这些行,然后split(/\s+/)
> 将行放入数组中。然后按索引提取您感兴趣的列。If none of the data columns will contain spaces, you could easily
split(/\n/)
the output into lines, iterate over those, andsplit(/\s+/)
the rows into arrays. Then extract the columns you are interested in, by index.