Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Checking my default gateway

by Piercer (Beadle)
on Aug 20, 2001 at 13:26 UTC ( #106158=perlquestion: print w/replies, xml ) Need Help??

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

A little background... I'm running a program which is currently just on windows but needs to be available to Unix and possibly some other OS's later. It checks the users TCP/IP default gateway and connects to a server in the vicinity of the user from this. My question... At the moment I run `ipconfig` and search the returned lines for the default gateway - this works fine for NT but thats OS specific (I think). Is there a Module which I can get this info from, I looked at ENV but couldn't get it from that - I'v searched CPAN but can't see anything which seems relevent. Thanks.

Replies are listed 'Best First'.
Re: Checking my default gateway
by idnopheq (Chaplain) on Aug 20, 2001 at 15:15 UTC
    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 ''. netstat should work on any TCP/IP enabled host ( YMMV ).

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

Re: Checking my default gateway
by claree0 (Hermit) on Aug 20, 2001 at 13:33 UTC

    How about using Net::Traceroute and using the output to give you the default GW?


      Sounds good, I'll try it out. Thanks
        net:traceroute doesn't seem to support Windoze - however I found Net::GrpNetworks which looks like it should do the trick. Cheers.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2023-09-22 01:58 GMT
Find Nodes?
    Voting Booth?

    No recent polls found