Thread RegEx für "ip addr show" (14 answers)
Opened by cbxk1xg at 2021-02-23 16:07

styx-cc
 2021-02-23 20:34
#193138 #193138
User since
2006-05-20
533 Artikel
BenutzerIn

user image
Ganz übersehen/unbedacht gelassen...

Als Ansatz, ist aber ncoh ein kleiner Fehler drin..
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my $str = << "EOM";
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether xx:xx:eb:xx:xx:xx brd ff:ff:ff:ff:ff:ff
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.178.22/24 brd 192.168.178.255 scope global dynamic noprefixroute wlan0
       valid_lft 861425sec preferred_lft 753425sec
    inet6 ffff::ffff:ffff:ffff:ffff/64 scope link
       valid_lft forever preferred_lft forever
EOM

my %interfaces= ();

while ($str =~ /(\d{1,2}\: (.+)\: \<(.+)\>.*mtu (\d+).*state (UP|DOWN|UNKNOWN).*\n.*|\n.*inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/\d{1,2})/g) {
        $interfaces{$2} = {link => $1, abilities => $3, mtu => $4, state => $5 };
}

print Dumper %interfaces;
Pörl.

View full thread RegEx für "ip addr show"