如何在 Perl 中打印出可用的以太网网卡?
我想知道是否可以实际打印出 Linux 机器上存在的可用以太网卡?到目前为止,我已经从“ifconfig”命令中读到了有关“Grep”的内容。
我必须使用“Net::IP”或“IO::Socket”吗?我是这种编程语言的新手,所以有人可以就如何做到这一点提供一些建议吗?
抱歉让你们困惑了!但我需要脚本在执行 perl 脚本后直接在终端打印“eth0”或“eth1”。
谢谢!
I was wondering if it's possible to actually print out the available ethernet cards that is present on a linux machine? I have so far read about "Grep" from the "ifconfig" command.
Do I have to use use "Net::IP" or "IO::Socket"? I am new to this programming language so can someone please give some advise on how to do this?
Sorry for confusing you guys! But I need the script to print like "eth0" or "eth1" directly at the terminal after excecuting the perl script.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看 Net::Address::Ethernet。
它可能只是 ifconfig/ipconfig 的一个便携式包装。不过,我不会太担心这一点,因为在任何情况下你都无法避免在这里调用操作系统 - 所以调用 ifconfig 可能和任何其他选项一样好。
还有一些其他模块可能值得一看。
UPD。如果您只需要接口名称,您还可以读取文件 /proc/net/dev。现代的 Linux 应该有它,而且文件结构很容易解析。
Have a look at Net::Address::Ethernet.
It might be just a portable wrapper around ifconfig/ipconfig. Though, I wouldn't worry much about this, because in any case you can't avoid a call to OS here - so calling ifconfig is probably as good as any other option.
There are also a few other modules probably worth looking at.
UPD. If you need interface names only, you could also read a file /proc/net/dev. A modern linux should have it, and the file structure is pretty easy to parse.
如果您仅针对 GNU/Linux 或 *BSD 进行编程,则可以使用“ifconfig”中的“grep”。
If you program for GNU/Linux or *BSD only, then you can use "grep" from "ifconfig".