"iproute" 크로스 플랫폼에서 로컬 IP 주소 가져오기

"iproute" 크로스 플랫폼에서 로컬 IP 주소 가져오기

크로스 플랫폼 명령을 사용하여 로컬 IP 주소를 추출하려고 합니다. 오늘까지 나는 다음 명령을 사용해 왔습니다.

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

그러나 출력이 다음 ip route get 1과 같기 때문에 Fedora 27에서는 작동하지 않습니다.

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 등을 얻기 위해 약 1년 전에 작성한 스크립트를 남겨드립니다. 자유롭게 수정해주세요. 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;}
    }


}

관련 정보