in reply to Checking my default gateway

Hi, All!

Even tho Piercer has his answer in Net::GrpNetworks, here is something I whipped up for NT/2K. It's modeled after *nix ifconfig command, as I detest having to scroll though the output of ipconfig on my multi-homed hosts. Note that the ifno ( i.e. eth0 ) is a totally made-up identifier. I planned on making the identifier more like 'windump -D''s output, but never got to it.

This ifconfig takes no options.

#!/usr/bin/perl -w #-*-perl-*- use strict; use Win32::TieRegistry; use Net::Netmask; use Socket; my %card; my %regKeys; $Registry->Delimiter("/"); my $network = $Registry->{"LMachine/Software/Microsoft/Windows NT/ +"}; $network = $network->{"CurrentVersion/NetworkCards/"}; my %cardInfo; my $netCard; foreach ( keys %$network ) { my $foo = $network->{$_}; $card{$foo->GetValue('ServiceName')} = $foo->GetValue('Description +'); } foreach $netCard ( keys %card ) { my $Key = $Registry->{"/LMachine/SYSTEM/CurrentControlSet/Serv +ices/"}; $Key = $Key->{"Tcpip/Parameters/Interfaces/$netCard/"}; $cardInfo{'dhcp'} = $Key->GetValue("EnableDHCP"); if ( $cardInfo{'dhcp'} eq "0x00000000" ) { $cardInfo{'ipAddr'} = $Key->GetValue("IPAddress"); $cardInfo{'mask'} = $Key->GetValue("SubnetMask"); $cardInfo{'domain'} = $Key->GetValue("Domain") || ""; $cardInfo{'nameServer'} = $Key->GetValue("NameServer") || ""; $cardInfo{'dhcp'} = 'no'; $cardInfo{'gateway'} = $Key->GetValue("DefaultGateway") || ""; $cardInfo{'metric'} = $Key->GetValue("DefaultGatewayMetric") || '1'; } else { $cardInfo{'ipAddr'} = $Key->GetValue("DhcpIPAddress"); $cardInfo{'mask'} = $Key->GetValue("DhcpSubnetMask"); $cardInfo{'domain'} = $Key->GetValue("DhcpDomain") || ""; $cardInfo{'nameServer'} = $Key->GetValue("DhcpNameServer") || ""; $cardInfo{'dhcp'} = 'yes'; $cardInfo{'gateway'} = $Key->GetValue("DhcpDefaultGateway") || +""; $cardInfo{'metric'} = $Key->GetValue("DhcpDefaultGatewayMetric +") || '1'; } $cardInfo{'ipAddr'} = removePad ( $cardInfo{'ipAddr'} ); $cardInfo{'mask'} = removePad ( $cardInfo{'mask'} ); my $block = new Net::Netmask ( $cardInfo{'ipAddr'}, $cardInfo{'mask'} ); $cardInfo{'broadcast'} = $block->broadcast(); $cardInfo{'mtu'} = $Key->GetValue("MTU") || 1500; my $type = $Registry->{"LMachine/SYSTEM/CurrentControlSet/Contr +ol/"}; $type = $type->{"Class/{4D36E972-E325-11CE-BFC1-08002BE10318 +}"}; my @members = $type->SubKeyNames; foreach ( @members ) { my $corge = $type->{"$_"}; if ( $corge->GetValue("NetCfgInstanceId") eq $netCard ) { $cardInfo{'flags'} = $corge->GetValue("Characteristics"); my $baz = $corge->{"Ndi/Interfaces/"}; $cardInfo{'media'} = $baz->GetValue("LowerRange"); $cardInfo{'ifno'} = $_; $cardInfo{'ifno'} =~ s/^0+//; $cardInfo{'ifno'} = 0 if $cardInfo{'ifno'} eq ""; $cardInfo{'ifno'} = ( substr $cardInfo{'media'}, 0, 3 ) . $c +ardInfo{'ifno'}; } } my $netBt = $Registry->{"LMachine/SYSTEM/CurrentControlSet/Servi +ces/"}; $netBt = $netBt->{"NetBT/Parameters/Interfaces/Tcpip_$netCard +/"}; $cardInfo{'wins'} = $netBt->GetValue("NameServerList") || "" +; write; } undef $Registry; exit 0; format STDOUT = @>>>>: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<< $cardInfo{'ifno'},$card{$netCard} Link encap:@<<<<<<<<<<<<< DHCP:@<< flags:@<<<<<<<<<<<<<<<<< +< $cardInfo{'media'}, $cardInfo{'dhcp' +}, $cardInfo{'flags'} inet addr:@<<<<<<<<<<<<<< Bcast:@<<<<<<<<<<<<<< Mask:@<<<<<< +<<<<<<<<< $cardInfo{'ipAddr'}, $cardInfo{'b +roadcast'}, $cardInfo{'mask'} gateway:@<<<<<<<<<<<<<<<< MTU:@<<< Metric:@<<< WINS:@<<<<<< +<<<<<<<<< $cardInfo{'gateway'}, $cardInfo{'mtu'}, + $cardInfo{'metric'}, $cardInfo{'wins'} domain:@<<<<<<<<<<<<<<<<< DNS:@<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +< $cardInfo{'domain'}, $cardInfo{'nameSer +ver'} . sub removePad { my $value = shift; $value =~ s/\x00+$//; return $value; } __END__

Anyway, it's an old script. A little digging in TechNet may make it more portable w/i M$ boxen. I know Solaris has it's '/etc/defaultrouter' file which can give the info ( if dynamic routing isn't in play ).

Another, more portable - non-perl, way is the 'netstat -r' command, and parse the output for 'default' or '0.0.0.0'. netstat should work on any TCP/IP enabled host ( YMMV ).

HTH
--
idnopheq
Apply yourself to new problems without preparation, develop confidence in your ability to to meet situations as they arrise.