Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 23rd, 2012, 07:33 AM
lakshmikant lakshmikant is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2010
Posts: 12 lakshmikant User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 59 m 6 sec
Reputation 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

Reply With Quote
  #2  
Old August 23rd, 2012, 12:46 PM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 549 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 5 Days 3 h 10 m 32 sec
Reputation Power: 406
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:
Original - Perl Code
  1. if ($line =~ /Link encap:/ ) {
  2.      # do what you want in this case
  3. }


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


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

Perl Code:
Original - Perl Code
  1. 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.

Reply With Quote
  #3  
Old August 23rd, 2012, 02:02 PM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,683 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 22 h 38 m 25 sec
Reputation Power: 1170
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)

Reply With Quote
  #4  
Old August 23rd, 2012, 09:30 PM
lakshmikant lakshmikant is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2010
Posts: 12 lakshmikant User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 59 m 6 sec
Reputation Power: 0
Quote:
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 "

Reply With Quote
  #5  
Old August 24th, 2012, 08:49 AM
FishMonger FishMonger is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2009
Posts: 1,683 FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level)FishMonger User rank is General 3rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 22 h 38 m 25 sec
Reputation Power: 1170
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";
    }
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > How to check and execute the condition

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap