#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0

    How to check and execute the condition


    eth0 Link encap:Ethernet HWaddr 00:0C:29:0F:9B:82
    inet addr:192.168.8.76 Bcast:192.168.8.255 Mask:255.255.25
    +5.0
    inet6 addr: fe80::20c:29ff:fe0f:9b82/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:280252 errors:0 dropped:0 overruns:0 frame:0
    TX packets:106184 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:35501675 (33.8 MiB) TX bytes:11696548 (11.1 MiB)
    Base address:0x1080 Memory:f4820000-f4840000

    lo Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    inet6 addr: ::1/128 Scope:Host
    UP LOOPBACK RUNNING MTU:16436 Metric:1
    RX packets:4664 errors:0 dropped:0 overruns:0 frame:0
    TX packets:4664 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:7952486 (7.5 MiB) TX bytes:7952486 (7.5 MiB)


    my script file look like this

    open FILE,"config.txt" or die "cannot open file : $!";
    my $interface; # declare the variable
    while(<FILE>) {

    $interface = $1 if /^([\S]+)/;
    print "$interface => $1\n" if /inet addr[0-9.]+)/;

    }

    need help I need to check the condition if line contain "Link encap:" then get the same output as shown else its say "not ethernet" but its not happening

    my output is:

    eth0 => 192.168.8.76
    lo => 127.0.0.1
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Location
    Paris area, France
    Posts
    843
    Rep Power
    496
    I do not really understand what you want to do, but if you want to check whether the line contains "Link encap:", you can simply use a regular expression like this:

    Perl Code:
    if ($line =~ /Link encap:/ ) {
         # do what you want in this case
    }


    Or, if your line is in $_, simply:
    Perl Code:
    if (  /Link encap:/ ) {
         # do what you want in this case
    }


    You could also use the index function, which is presumably faster

    Perl Code:
    my $position = index $line, "Link encap:";

    If the substring is not found, index returns -1.
    If index returns 0 or a positive value, that the string has been found.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,968
    Rep Power
    1225
    Code:
    use strict;
    use warnings;
    
    $/ = '';
    
    my %interfaces;
    while (my $interface = <DATA>) {
        if ($interface =~ /(\S+)\s+Link encap:/) {
            my $int = $1;
            ( $interfaces{$int} ) = $interface =~ /inet addr:(\S+)/;
        }
        else {
            print "Not Ethernet\n";
        }
    }
    
    foreach my $interface ( sort keys %interfaces ) {
        printf "%-6s => %s\n", $interface, $interfaces{$interface};
    }
    
    
    __DATA__
    eth0 Link encap:Ethernet HWaddr 00:0C:29:0F:9B:82
    inet addr:192.168.8.76 Bcast:192.168.8.255 Mask:255.255.25
    +5.0
    inet6 addr: fe80::20c:29ff:fe0f:9b82/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:280252 errors:0 dropped:0 overruns:0 frame:0
    TX packets:106184 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:35501675 (33.8 MiB) TX bytes:11696548 (11.1 MiB)
    Base address:0x1080 Memory:f4820000-f4840000
    
    lo Link encap:Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    inet6 addr: ::1/128 Scope:Host
    UP LOOPBACK RUNNING MTU:16436 Metric:1
    RX packets:4664 errors:0 dropped:0 overruns:0 frame:0
    TX packets:4664 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:7952486 (7.5 MiB) TX bytes:7952486 (7.5 MiB)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    12
    Rep Power
    0
    Originally Posted by FishMonger
    Code:
    use strict;
    use warnings;
    
    $/ = '';
    
    my %interfaces;
    while (my $interface = <DATA>) {
        if ($interface =~ /(\S+)\s+Link encap:/) {
            my $int = $1;
            ( $interfaces{$int} ) = $interface =~ /inet addr:(\S+)/;
        }
        else {
            print "Not Ethernet\n";
        }
    }
    
    foreach my $interface ( sort keys %interfaces ) {
        printf "%-6s => %s\n", $interface, $interfaces{$interface};
    }
    
    
    __DATA__
    eth0 Link encap:Ethernet HWaddr 00:0C:29:0F:9B:82
    inet addr:192.168.8.76 Bcast:192.168.8.255 Mask:255.255.25
    +5.0
    inet6 addr: fe80::20c:29ff:fe0f:9b82/64 Scope:Link
    UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
    RX packets:280252 errors:0 dropped:0 overruns:0 frame:0
    TX packets:106184 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:35501675 (33.8 MiB) TX bytes:11696548 (11.1 MiB)
    Base address:0x1080 Memory:f4820000-f4840000
    
    lo Link encap:Local Loopback
    inet addr:127.0.0.1 Mask:255.0.0.0
    inet6 addr: ::1/128 Scope:Host
    UP LOOPBACK RUNNING MTU:16436 Metric:1
    RX packets:4664 errors:0 dropped:0 overruns:0 frame:0
    TX packets:4664 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:0
    RX bytes:7952486 (7.5 MiB) TX bytes:7952486 (7.5 MiB)
    I need small modification
    instead of saying "Not Ethernet" how to say "(ethernet name) is not ethernet "
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,968
    Rep Power
    1225
    Do you want the loopback interface included in the list of eth interfaces, or should it be in the "not Ethernet" list?

    IMO, the loopback should not be in the eth list, but your opening post indicates that you do want it listed.

    Shouldn't the only eth interfaces listed be the "Link encap:Ethernet" ones?

    Code:
    while (my $interface = <DATA>) {
        my ($int) = $interface =~ /^(\S+)/;
        if ($interface =~ /Link encap:Ethernet/) {
        ( $interfaces{$int} ) = $interface =~ /inet addr:(\S+)/;
        }
        else {
            print "$int is not Ethernet\n";
        }
    }

IMN logo majestic logo threadwatch logo seochat tools logo