Jump to content

First try - _NetGetDefaultGateway

Recommended Posts

This is in response to the posting Get Default Gateway, How to simply get the default Gateway... posted by Julien.alkaza.

I've written a UDF that gets the default gateway info via WMI. Here's a summary of the function:

; Function: _NetGetDefaultGateway  ( $sNIC_Description='', $iMatch=3, $iNulGateways=0, $strComputer='localhost' )
; Paramaters:
;   $sNIC_Description - Name of NIC to query.  Default = '' for all active NICs
;   $iMatch - How to match the NIC string. Default is 3 (match any part of NIC name)
;       0 = Exact Match
;       1 = Match from the beginning of the NIC name passed (NicName*)
;       2 = Match from the end of the NIC name passed (*NicName)
;       3 = Match any part of NIC name passed (*NicName*)
;       4 = Do NOT match any part of NIC name passed ( <> *NicName* ) !!! DOES NOT WORK YET !!!
;   $iNulGateways - Allow NUL Gateways. Default = 0
;       0 = Do NOT allow NUL gateways
;       1 = ALLOW NUL gateways
;   $strComputer - Computer to query. Default is 'localhost'
; Returns two dimensional array:
;   element [0][0] contains the number of Gateways found
;   element [x][0] contains the description (Name) of the NIC
;   element [x][1] contains the gateway for that NIC
; Error Returns:
;   @error = 1 - WMI Object could not be created
;   @error = 2 - Collection Object could not be created
;   @error = 9 - No Gateways Found
; ------
;   1. As noted above, requesting all EXCEPT ($iMatch=4) does NOT work (yet?)
;   2. A value of -1 can be used instead of default integer values to make it
;      easier to use them.

I'm having one small problem with it, and one question about how things are handled.

First, when you look at the code, everything works except if you try to use do NOT match ($iMatch=4), but I can't figure out why it's not working. In the code you'll see there's a REM'd out MsgBox that will display the WMI Query string, and it looks like it should work, but doesn't. Then again I don't really know how that's supposed to be done, so if anyone out there knows how to fix that, it would be greatly appreciated.

There is a test script before the function which references 'intel' and 'vm' NICs. The default (only real) NIC on my machine is an Intel NIC, and I have VMWare which has two NICs that have no gateway. The last test code of the function gets all the active default gateways of another computer - change 'ADIFFERENTCOMPUTER' to another computer on your network, or your computer name to test it.

The reason I'd like to be able to have a switch to return Gateways from all NICs EXCEPT ones with ??? in their name is to be able to use _NetGetDefaultGateway ( 'wireless', 4 ) to return all active gateways except those associated with any wireless NICs. Originally the function only returned a one dimensional array with the gateway addresses, but I decided to make it a two dimensional array so I'd be able to do this using the return values.

The other question I have is this. I decided to set an error when no gateways were found, but really there's no error. I suppose you can just look at the value of the [0][0] element to find out, but then figured I'd ask how this is usually handled.

This is my first code post, and NOT ready for prime time - I HAVE read the UDS standards documents, and have NOT yet fulfilled those requirements.

In addition to the impetus from Julien.alkaza, I came across the The Do-It-Yourself Script Center Kit at Microsoft's Scripting Center (download page for do_it_yourself_kit.exe ). Once you set up your machine so the scripts can work, it will generate no less than 1,627 VB Scripts organized in 458 directories! You have to create the directory C:\CODE for code_folder_maker.vbs to work, then run code_maker.vbs to generate the code.

This script can also be easily modified to get other info too like MAC address, etc.

Your (on topic) feedback is welcome. Thanks!


UDF: http://www.autoitscript.com/fileman/users/public/JerryD/_NetGetDefaultGateway.au3

Edited by JerryD
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...