Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Change Windows DNS Settings

by Mr_Person (Hermit)
on Mar 05, 2003 at 00:02 UTC ( #240490=perlquestion: print w/replies, xml ) Need Help??

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

Is there a way with Perl to change the DNS settings on Windows (98 and 2000)? I need to change the secondary name server address on quite a few PC's because we're moving the nameserver to a different subnet. Most of them can be updated through DHCP, but for various reasons there are about 50 that don't use DHCP. I can have them run a Perl script on bootup through login scripts and was wondering if there was some way (Windows API?) to change that setting automatically instead of going around to each PC and changing it myself.


Replies are listed 'Best First'.
Re: Change Windows DNS Settings
by tachyon (Chancellor) on Mar 05, 2003 at 00:29 UTC

    Here is some VBS that sets all the network settings except DNS - can't do all your work for you. I will leave it up to you to get the required object and property and turn this into Win32::OLE code if you really want. Here is a hint You DO KNOW that the logon script will run locally so if you want it to be in perl your target machines will need perl installed or you will need to make the script and exe....

    strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cim +v2") Set colNetAdapters = objWMIService.ExecQuery _ ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TR +UE") strIPAddress = Array("") strSubnetMask = Array("") strGateway = Array("") strGatewayMetric = Array(1) For Each objNetAdapter in colNetAdapters errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask +) errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetr +ic) If errEnable = 0 Then WScript.Echo "The IP address has been changed." Else WScript.Echo "The IP address could not be changed." End If Next



Re: Change Windows DNS Settings
by Rex(Wrecks) (Curate) on Mar 05, 2003 at 00:49 UTC
    Ok, this won't work for Win9x, but a much more robust solution for Win2k/XP is the following:
    netsh interface ip add dns "Local Area Connection" index=1 netsh interface ip delete dns "Local Area Connection"
    Look at the netsh for more options and details. There is a ton of stuff you can do with netsh including changing IP's, netmasks, routes, adding deleting (non-LAN) interfaces, etc.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: Change Windows DNS Settings
by meetraz (Hermit) on Mar 05, 2003 at 21:36 UTC
    If you want to use Perl, using Win32::OLE and WMI is the way to go. You can use
    the SetDNSServerSearchOrder() method of the Win32_NetworkAdapterConfiguration class
    in order to set up your DNS.

    The link tachyon gave is for changing settings on a windows DNS Server,
    not for changing DNS settings for a Win98/2000 workstation. You don't need to install
    anything additional to change workstation settings, just use the class method I mentioned above.

    If you want this to run from a login script, and your users have admin rights on their workstations,
    using netsh commands as given by Rex(Wrecks) is the best way to go.

Re: Change Windows DNS Settings
by Mr_Person (Hermit) on Mar 05, 2003 at 22:47 UTC

    Thanks for all the great advice. I think I'll just use the netsh command for the Windows 2000 users and since the DNS Settings for Windows 98 are always in the same place in the registry, I'll just modify that directly.

    Thanks again!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2020-07-02 08:22 GMT
Find Nodes?
    Voting Booth?

    No recent polls found