ip addr show
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
(\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})
1 2 3 4 5 6 7 8 9
#!/usr/bin/perl use strict; use warnings; my $netStats = ""; $netStats = `ip addr show`; # Ich hatte erst überlegt das Ergebnis aufzuteilen. Aber auch das funktioniert leider nicht. Und ich finde es un-elegant. my @networksDevices = split (m/(\d{1,2}\: .+\n(\s{3,}.*\n*)+)/i, $netStats);
1 2
my $netStats = `ip addr show`; my @interfaces = split /\n(?=\s*\d+:\s*)/,$netStats;
2021-02-23T19:01:36 hajDamit bekommst Du ein interface in den Hash, aber nicht alle :)
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;
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
#! /usr/bin/env perl use strict; use warnings; use Data::Dumper; my $cmd = "ip addr show"; my %interface; open my $pipe, '-|', $cmd or die "(E) could not execute: $cmd: $!\n"; while ( my $line = <$pipe> ) { if ( $line =~ m/(\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})/ ) { $interface{$2}->{link} = $1; $interface{$2}->{abilities} = $3; $interface{$2}->{mtu} = $4; $interface{$2}->{state} = $5; } } close $pipe; warn Dumper \%interface;
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
#! /usr/bin/env perl use strict; use warnings; use Data::Dumper; my $cmd = "ip addr show"; my %interface; open my $pipe, '-|', $cmd or die "(E) could not execute: $cmd: $!\n"; my $int; while ( my $line = <$pipe> ) { if ( $line =~ m/^\d+:\s+([^:]+): <([^>]+)>/ ) { $int = $1; $interface{$int}->{abilities} = $2; } while ( $line =~ m/(mtu|state|inet6?) (\S+)/g ) { $interface{$int}->{$1} = $2; } } warn Dumper \%interface; close $pipe;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
my $netStats = ""; $netStats = `ip addr show`; my @networksDevices = split (m/^\d{1,2}\:\s/gm, $netStats); shift @networksDevices; my %interfaces; for my $device (@networksDevices) { #print Dumper($device); if ($device =~ /^(.+)\: \<(.+)\>.*mtu (\d+).*state (UP|DOWN|UNKNOWN)/gm) { %interfaces = ( $1 => {options => $2, mtu => $3, state => $4 }); } if ($device =~ /inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/\d{1,2}/gm) { #print Dumper($1); } } print Dumper(%interfaces);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
my $cmd = "ip addr show"; my %interfaces; open my $pipe, '-|', $cmd or die "(E) could not execute: $cmd: $!\n"; my $device; while ( my $line = <$pipe> ) { if ( $line =~ m/^\d+:\s+([^:]+): <([^>]+)>/ ) { $device = $1; $interfaces{$device}->{abilities} = $2; } while ( $line =~ m/(mtu|state|qlen|brd?) (\S+)/g ) { $interfaces{$device}->{$1} = $2; } } close $pipe; # stash für JSON view $c->stash->{networkDevices} = \%interfaces;
QuoteCode (perl): (dl )$line =~ m/(mtu|state|inet6?) (\S+)/g
ip --oneline addr show