Linux 如何在 Perl 中找到每个接口的 IP 地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4101219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:53:27  来源:igfitidea点击:

How can I find the IP addresses for each interface, in Perl?

linuxperlnetworkingcentos

提问by jinanwow

I am trying to find a list of IP addresses on a linux box. Currently my setup is a CentOS machine with a few sub interfaces for eth0 for each VLAN. I am writing a script to see if each VLAN IP address has connectivity to certain IP addresses (different IP addresses for each network).

我正在尝试在 linux 机器上查找 IP 地址列表。目前我的设置是一台 CentOS 机器,每个 VLAN 的 eth0 都有几个子接口。我正在编写一个脚本来查看每个 VLAN IP 地址是否连接到某些 IP 地址(每个网络的不同 IP 地址)。

For example:

例如:

  • eth0 has an IP of 10.0.0.2 netmask 255.255.255.128

  • eth0.2 has an IP of 10.0.130 netmask 255.255.255.128

  • eth0.3 has an IP of 10.0.1.2 netmask 255.255.255.128

  • eth0 的 IP 为 10.0.0.2 网络掩码 255.255.255.128

  • eth0.2 的 IP 为 10.0.130 网络掩码 255.255.255.128

  • eth0.3 的 IP 为 10.0.1.2 网络掩码 255.255.255.128

Each interface is currently set to static IP address via config files. However, I am wanting to change it from static to DHCP and get the same IP address. If I do this, it will break this part of the script:

每个接口当前都通过配置文件设置为静态 IP 地址。但是,我想将其从静态更改为 DHCP 并获得相同的 IP 地址。如果我这样做,它将破坏脚本的这一部分:

@devarray = `cat /etc/sysconfig/network-scripts/ifcfg-eth0* | grep IPADDR=10 -B 10 | grep -v "#" | grep IPADDR`;

Is there a better way of determining what IP addresses are avaiable. All I need to gather is just the IP address and not the device name.

是否有更好的方法来确定哪些 IP 地址可用。我需要收集的只是 IP 地址而不是设备名称。

采纳答案by Weegee

If you want a pure Perl solution, you might try IO::Interface. I've had some success with it in the past, and the documentation is good.

如果你想要一个纯 Perl 解决方案,你可以尝试IO::Interface。我在过去取得了一些成功,而且文档很好。

回答by Mike Axiak

How about using ifconfig?

使用ifconfig怎么样?

my @ips = (`ifconfig -a` =~ /inet addr:(\S+)/g);

回答by phaylon

There is Net::Interface, which seems to give me good results on my system:

Net::Interface,它似乎在我的系统上给了我很好的结果:

my %addresses = map { 
      ($_ => [
          map { Net::Interface::inet_ntoa($_) } # make addresses readable
              $_->address,                      # all addresses
      ]);
} Net::Interface->interfaces;                   # all interfaces

This will return something like

这将返回类似

(
  eth0 => [],
  eth1 => [ '192.168.2.100' ],
  lo   => [ '127.0.0.1' ]
)

Update: Should've mentioned: Check the documentation for methods other than addressto get at other information per interface.

更新:应该提到:检查文档以了解除address获取每个接口的其他信息之外的方法。

回答by Lux In Tenebris

I think that the following method is the most reliable and environment-independent. But only useful if you known an interface name.

我认为以下方法是最可靠且与环境无关的。但仅当您知道接口名称时才有用。

#!/usr/bin/perl
use strict;
use warnings;
use Socket;
require 'sys/ioctl.ph';

print get_interface_address('eth0');

sub get_interface_address
{
    my ($iface) = @_;
    my $socket;
    socket($socket, PF_INET, SOCK_STREAM, (getprotobyname('tcp'))[2]) || die "unable to create a socket: $!\n";
    my $buf = pack('a256', $iface);
    if (ioctl($socket, SIOCGIFADDR(), $buf) && (my @address = unpack('x20 C4', $buf)))
    {
        return join('.', @address);
    }
    return undef;
}

found here: http://snipplr.com/view/46170/the-most-reliable-and-correct-method-to-get-network-interface-address-in-linux-using-perl/

在这里找到:http: //snipplr.com/view/46170/the-most-reliable-and-correct-method-to-get-network-interface-address-in-linux-using-perl/