Wie lese ich NIC-Parameter unter Windows aus?

Inhalt:

NIC Parameter auslesen

-- RemoHehlert - 04 Apr 2005

English Version below

Deutsch:

Um Parameter über die Netzwerkkarte (NIC = Network Interface Card) herauszufinden gibt es sicherlich mehrere Möglichkeiten! Die hier eingesetzten Module beziehen sich auf Win32::IPHelper und Win32::TieRegistry. Das beigefügte Skript liest die Daten aus der Registry aus und zeigt die Ausgabe CMD Konsole von Windows an. Das Skript arbeitet ausschließlich auf WINDOWS und wurde unter folgende Systemen getestet:

WINNT SP6; W2K? SP4; WIN XP SP2

Read NIC parameters

English:

to get the parameter about your Networkcard (NIC = Network Interface Card) there are several ways to find out them. The Modules we are using here are Win32::IPHelper and Win32::TieRegistry. This Script read the data from the registry and display the data on CMD console from your Windows. The Script runs only on WINDOWS and is tested under:

WINNT SP6; W2K? SP4; WIN XP SP2

Script nic_info.pl

################################################################################
#                                                                              
# AUTHOR           : Remo Hehlert                                             
# CREATION DATE    : 30 March 2005                                         
#                                                                             
# SHORT DESCRIPTION: find out NIC configuration 
#                                                                             
# (c) R.H. 2004 - 2005                        
#                                                                             
# -----------------------------------------------------------------------------
# File name        : nic_info.pl
# Location         : FRA
# Last edited by   : Remo Hehlert
# Last Checkin     : 30.03.2005
# Revision         : 0.1
# -----------------------------------------------------------------------------
# nic_info.pl, global file description
# -----------------------------------------------------------------------------
# History:
# ??.??.2005 
# ??.??.2005 
# 30.01.2005 first version
# -----------------------------------------------------------------------------
#
# Last change: 30.03.2005 Remo Hehlert
#
################################################################################
#
#
# ------------------------------------------------------------------
# Loaded Modules
# ------------------------------------------------------------------
use strict;
use Data::Dumper;
use Win32::TieRegistry( Delimiter=>"/");
#
#
# ------------------------------------------------------------------
# find out operating System
# ------------------------------------------------------------------
my $os = `ver`; 
$os =~ s/[ \n]//g;

if ($os eq "WindowsNTVersion4.0")
  {
    &os_winnt;
  }
  else 
      {
       &other_win;
      }
#
#
# ------------------------------------------------------------------
# get parm. for windows NT4
# ------------------------------------------------------------------
sub os_winnt
   {
    my $Class = $Registry->{"LMachine/SYSTEM/CurrentControlSet/Services"} ||  
                             die "Can't read LMachine/SYSTEM/CurrentControlSet/Services key: $^E\n";
    my $NetKey;
       foreach my $SubKey (keys %{$Class}) 
              {
               my ( $value, $type )= $Class->{$SubKey}->{"/Group"};
               if (defined $value && $value eq 'NDIS') 
                 {
                   $NetKey = $Class->{$SubKey};
                   $SubKey =~ s/[ \/]//g;
                   &get_parm($SubKey);
                   last;
                 }
              }
   }
sub get_parm 
   {
    my ($SubKey) = @_;
    my $num = "1";
    my $nic = "$SubKey$num";
    if (my $Class = $Registry->{"LMachine/SYSTEM/CurrentControlSet/Services/$nic/Parameters"})
      {
        &go_next($Class);
      }
      else 
          {
           my $num = "2";
           my $nic = "$SubKey$num";
           my $Class = $Registry->{"LMachine/SYSTEM/CurrentControlSet/Services/$nic/Parameters"};
           &go_next($Class);
          }
          
   }

sub go_next
   {
    my ($Class) = @_;
    foreach (keys %$Class) 
           {
             my $parm = "$_:\t $Class->{$_}";
             $parm =~ s/[\/]//g;
             print $parm,"\n";
           }
   }
#
#
# ------------------------------------------------------------------
# get parm. for windows W2K and XP
# ------------------------------------------------------------------
sub other_win
   {
    use Win32::IPHelper;
# ------------------------------------------------------------------
# get UUID form NIC
# ------------------------------------------------------------------
    my @IP_ADAPTER_INFO;
    my $data;
    my $adp_string;
    my $ret = Win32::IPHelper::GetAdaptersInfo(\@IP_ADAPTER_INFO);
    
    if ($ret == 0)
      {
        $data = Data::Dumper->Dump([\@IP_ADAPTER_INFO], [qw(IP_ADAPTER_INFO)]);
        my $string = $data=~ /'AdapterName' => '{(.*?)}'/;
        $adp_string = $1;
        print $adp_string,"\n"; #UUID
        &int_info();
      }
      else
          {
           printf "GetAdaptersInfo() error %u: %s\n", $ret, Win32::FormatMessage($ret);
          }
#
#
# ------------------------------------------------------------------
# get NIC current speed and type
# ------------------------------------------------------------------
sub int_info
   {
    my $IfIndex;
    my $AdapterName = "{$adp_string}";
    my $ret = Win32::IPHelper::GetAdapterIndex(\$AdapterName,\$IfIndex);
    if ($ret == 0)
      {
        my %MIB_IFROW;
        $ret = Win32::IPHelper::GetIfEntry($IfIndex,\%MIB_IFROW);
        
        if ($ret == 0)
          {
            my $data = Data::Dumper->Dump([\%MIB_IFROW], [qw(MIB_IFROW)]);
            my $speed = $data=~ /'Speed' =>(.*?),/;
            $speed = $1;
            my $adp_type = $data=~ /'Descr' =>(.*?),/;
            $adp_type = $1;
            print "\n$data\ncurrent speed = $speed\nAdapter Type = $adp_type\n";
          }
          else
              {
               printf "GetIfEntry() error %u: %s\n", $ret, Win32::FormatMessage($ret);
              }
      }
      else
          {
           printf "GetAdapterIndex() error %u: %s\n", $ret, Win32::FormatMessage($ret);
          }
    &go_data();
   }
#
#
# ------------------------------------------------------------------
# get NIC parm.
# ------------------------------------------------------------------
sub go_data
   {
    my $Class = $Registry->{"LMachine/SYSTEM/ControlSet001/Control/Class"} ||  
                die "Can't read LMachine/SYSTEM/ControlSet001/Control/Class key: $^E\n";
    my $NetKey;
    foreach my $SubKey (keys %{$Class}) 
           {
            my ( $value, $type )= $Class->{$SubKey}->{"/Class"};
            if (defined $value && $value eq 'Net') 
              {
                $NetKey = $Class->{$SubKey};
                last;
              }
           }
           my $AdapterKey;
           foreach my $SubKey (keys %{$NetKey}) 
                  {
                   my ( $value, $type )= $NetKey->{$SubKey}->{"/NetCfgInstanceId"};
                   if (defined $value && $value eq "{$adp_string}") 
                     {
                       $AdapterKey = $NetKey->{$SubKey};
                       last;
                     }
                  }
                  foreach my $SubKey (keys %{$AdapterKey->{'Ndi'}->{'params'}}) 
                         {
                          my $pKey = $SubKey;
                          chop $pKey;
                          my ( $value, $type )= $AdapterKey->{$pKey};
                          my ( $paramname, $ptype )= $AdapterKey->{'Ndi'}->{'params'}->{$SubKey}->{"/ParamDesc"};
                          print "$paramname: $value\n";
                         }
   }
}
#
#

Ergänzungen, Kommentare

Kommentare werden am besten in folgender Form vorgenommen, damit sie im Inhaltsverzeichnis angezeigt werden (natürlich ohne das <verbatim>):
---+++ Main.??? - 14 Jul 2003 - Betreff

UtilFaqSubForm edit

Titel Wie lese ich NIC-Parameter unter Windows aus?
Autor RemoHehlert
Bereich FaqPerlUndWin32
Topic revision: 2005-04-04, ReneeBaecker
 
Bitte die NutzungsBedingungen beachten.
Bei Vorschlägen, Anfragen oder Problemen mit dem PerlCommunityWiki bitten wir um WebBottomBarExample">Rückmeldung.