简体   繁体   English

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

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

I am trying to find a list of IP addresses on a linux box. 我试图在linux盒子上找到一个IP地址列表。 Currently my setup is a CentOS machine with a few sub interfaces for eth0 for each VLAN. 目前我的设置是CentOS机器,每个VLAN都有几个用于eth0的子接口。 I am writing a script to see if each VLAN IP address has connectivity to certain IP addresses (different IP addresses for each network). 我正在编写一个脚本,以查看每个VLAN IP地址是否与某些IP地址(每个网络的不同IP地址)连接。

For example: 例如:

  • eth0 has an IP of 10.0.0.2 netmask 255.255.255.128 eth0的IP为10.0.0.2 netmask 255.255.255.128

  • eth0.2 has an IP of 10.0.130 netmask 255.255.255.128 eth0.2的IP为10.0.130,网络掩码为255.255.255.128

  • eth0.3 has an IP of 10.0.1.2 netmask 255.255.255.128 eth0.3的IP为10.0.1.2 netmask 255.255.255.128

Each interface is currently set to static IP address via config files. 每个接口当前通过配置文件设置为静态IP地址。 However, I am wanting to change it from static to DHCP and get the same IP address. 但是,我想将它从静态更改为DHCP并获得相同的IP地址。 If I do this, it will break this part of the script: 如果我这样做,它将破坏脚本的这一部分:

@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. 有没有更好的方法来确定可用的IP地址。 All I need to gather is just the IP address and not the device name. 我需要收集的只是IP地址而不是设备名称。

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 address to get at other information per interface. 更新 :应该提到:检查文档中除address以外的方法以获取每个接口的其他信息。

If you want a pure Perl solution, you might try IO::Interface . 如果您需要纯Perl解决方案,可以尝试IO :: Interface I've had some success with it in the past, and the documentation is good. 我以前在这方面取得了一些成功,而且文档很好。

使用ifconfig怎么样?

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

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/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM