Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How can I check if the eth? exist

by A_Banknote (Initiate)
on Jul 09, 2004 at 15:29 UTC ( [id://373167]=perlquestion: print w/replies, xml ) Need Help??

A_Banknote has asked for the wisdom of the Perl Monks concerning the following question:

I want to check how many net cards are there?what should I do?I think I cannot find them in 'ls /dev/*' . thx for help. edited: well, I mean, use perl to check if there is a eth? exsit. thx again.

Replies are listed 'Best First'.
Re: How can I check if the eth? exist
by blue_cowdawg (Monsignor) on Jul 09, 2004 at 15:48 UTC

        I want to check how many net cards are there?

    You didn't specify what OS you are on, but it looks like you are running on Linux given you are looking for "eth?".

    Here is a sniglet that I pulled out of an RC script of mine running on RH 9.0 and therefore tested on RH 9.0. There is enough common stuff here that it should run on any flavor of *nix and revolves around the netstat -i command invokation.

    #!/usr/bin/perl -w ################################ use strict; open(PIPE,'netstat -i|') or die $!; my $header=<PIPE>; # Kernel Interface Table $header=<PIPE>; # The real header chomp $header; # # Oh... what the hey... Let's parse it just for grins. my @fields=split(/[\s]+/,$header); my %ifTable=(); # Set up an empty assoc array while (my $line=<PIPE>){ chomp $line; my @f=split(/[\s]+/,$line); foreach my $ix(1..$#f){ $ifTable{$f[0]}->{$fields[$ix]}=$f[$ix]; } } # # But wait... you just wanted the IF names: printf "%s\n",join(",",keys %ifTable);

    when I ran it on my laptop (another flavor of Linux other than where it usually runs) it not only worked portably but I got the following:
    vmnet1,lo,wlan0,vmnet8,eth0

    Now, this lists the interfaces, if you wanted to just determine the existance of an interface you could also do something like:
    : : much handwaving here my @results=grep /eth1/,`netstat -i`; : : I didn't test that, but should work... :

    Using my much wordier code you could (instead of printing) also do:
    : : print "It's there\n" if $ifTable{'eth1'}; # or whatever : :

    Hope this is of some small use to you...

Re: How can I check if the eth? exist
by heroin_bob (Sexton) on Jul 09, 2004 at 15:40 UTC
    Here's some more info about ifconfig , which should give you a fair amount of information.
    ~hb
Re: How can I check if the eth? exist
by b10m (Vicar) on Jul 09, 2004 at 15:35 UTC

    ifconfig -a?

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: How can I check if the eth? exist
by haoess (Curate) on Jul 09, 2004 at 15:41 UTC

    If you use Linux, please have a look at /proc/net/dev. Nice exercise for (very) beginners (open file, parse content, output result).

    -- Frank

Re: How can I check if the eth? exist
by castaway (Parson) on Jul 09, 2004 at 19:40 UTC
Re: How can I check if the eth? exist
by rob_au (Abbot) on Jul 10, 2004 at 09:34 UTC
    While I've come to this discussion somewhat late, if you are wanting a more cross platform solution (as opposed to parsing the output of an external command such as ifconfig), you may wish to consider the employ of the most recent version of Net::Pcap (and the corresponding libpcap library) - With the most recent version of this module (0.05), a list of all network devices that can be used fornetwork monitoring can be retrieved using the findalldevs function:
    my @devs = Net::Pcap::findalldevs( \( my $err = undef ) )

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001011101000'"

Re: How can I check if the eth? exist
by A_Banknote (Initiate) on Jul 09, 2004 at 15:57 UTC
    thx all upon. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://373167]
Approved by b10m
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found