从“iproute”跨平台获取本地IP地址

从“iproute”跨平台获取本地IP地址

我正在尝试使用跨平台命令提取本地 IP 地址。直到今天,我一直在使用这个命令:

ip route get 1 | awk '{print $NF;exit}'

但在 Fedora 27 上不起作用,因为输出ip route get 1是:

0.0.0.1 via 192.168.1.1 dev en1  src 192.168.0.229 uid 1000
    cache

我正在获取1000IP 地址。在我尝试过的所有其他系统中,输出始终是:

0.0.0.1 via 192.168.1.1 dev en1  src 192.168.0.229

我也尝试使用此命令得到相同的结果:

ip route get 255.255.255.255 | sed -n '/src/ s/.*src //p'

答案1

要打印紧随其后的地址src(假设所有相关部分都位于同一行......):

ip route get 1 | sed 's/^.*src \([^ ]*\).*$/\1/;q'

答案2

尝试这个

ip route get 1 | awk '{print $7}'

答案3

也许这不是您正在寻找的,基本上是因为我使用ifconfig而不是使用新的命令形式ip来获得我想要的东西。即便如此,我还是给您留下了我大约一年前编写的一段脚本,用于获取 IP 等内容。请随意修改它。它是用 Perl 编写的,在 Debian 8 (Jessie) 中运行没有任何问题。

#!/usr/bin/perl -w

use strict;                                # strict style
use warnings;                              # activate warnings
use diagnostics;                           # diagnostic failover
use 5.010;
no warnings 'experimental';

if (!@ARGV) {
    print "\nPlease enter the interface name. ie: etho, wlan0, bond0...\n\n";
    exit();

}
# This piece is for avoid misspelling or other things
if ($ARGV[0] =~ s/[\$#;@~!&*()\[\];.,:?^ `\\\/]+//g) {
    print "\nWhat are you trying?\n\n";
    exit();
}


my $sentence = "ifconfig " . $ARGV[0]; 

my @ethernet = `$sentence `; 
my $length = scalar @ethernet;

for (my $i = 0; $i < $length; $i++) {

    given ($i) {

        #MAC address
        if ($i == 0) {
            if ($ethernet[$i] =~ /([0-9A-Fa-f][0-9A-Fa-f]([:-])[0-9A-Fa-f][0-9A-Fa-f](\2[0-9A-Fa-f][0-9A-Fa-f]){4})/ ) {
                my $mac_address = $1;
                print "The MAC address of $ARGV[0] is $mac_address\n";
            } break;}

        #IP address
        if ($i == 1) {
            if($ethernet[$i] =~ /([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/ ){
                my $ip_address = $1;
                print "The IP address of $ARGV[0] is $ip_address\n";
                    } break;}

        #MTU
        if ($i == 3) {
            if ($ethernet[$i] =~ /MTU:([^\sB]*)/ ) {
                my $mtu = $1;
                print "The MTU of $ARGV[0] is $mtu\n";
                }; break;}

        #Recieved package
        if ($i == 4) {
            if ($ethernet[$i] =~ /RX packets:([^\sB]*)/ ) {
                my $rx_pckg = $1;
                print "The amount of Recieved Package in $ARGV[0] is $rx_pckg\n";
                }; break;}      

        #Transmited package
        if ($i == 5) {
            if ($ethernet[$i] =~ /TX packets:([^\sB]*)/ ) {
                my $tx_pckg = $1;
                print "The amount of Transmited Package in $ARGV[0] is $tx_pckg\n";
                }; break;}      

        #Number of collisions
        if ($i == 6) {
            if ($ethernet[$i] =~ /collisions:([^\sB]*)/ ) {
                my $collisions = $1;
                print "The number of collisions in $ARGV[0] is $collisions\n";
                }; break;}

        #Total RX and TX in MB and GiB
        if ($i == 7) {
            if ($ethernet[$i] =~ /RX bytes:([^\sB]*)/ ) {
                my $rx_total_byte = $1;
                my $rx_total_mega = $rx_total_byte / 1048576;
                my $rx_total_giga = $rx_total_mega / 1024;
                print "The total amount of RecievedPackets in $ARGV[0] is $rx_total_mega Mb / $rx_total_giga GiB\n";
                }

            if ($ethernet[$i] =~ /TX bytes:([^\sB]*)/ ) {
                my $tx_total_byte = $1;
                my $tx_total_mega = $tx_total_byte / 1048576;
                my $tx_total_giga = $tx_total_mega / 1024;
                print "The total amount of RecievedPackets in $ARGV[0] is $tx_total_mega Mb / $tx_total_giga GiB\n";
                }; break;}
    }


}

相关内容