Sign in to follow this  
Followers 0
big_daddy

Help with script for setting DNS numbers

13 posts in this topic

Okay, so far I am able to set the DNS numbers on a machine with the netsh command. Now what I'm needing is for it to use one set of DNS numbers for a certain IP Address range and different set for a different IP range.

Example:

IP Address: 10.1.1.* 10.1.3.* 10.1.4.* 10.1.5.*

Will Get DNS Numbers:

10.1.1.2

10.1.1.5

10.1.2.5

IP Address: 10.1.2.*

Will Get DNS Numbers:

10.1.2.5

10.1.1.2

10.1.1.5

#include <GUIConstants.au3>
#include <misc.au3>
Global $IP = @IPAddress1

; Run this set of numbers if IP Address = 10.1.1.*  10.1.3.*  10.1.4.*  10.1.5.*
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.2 index=1', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.5 index=2', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.2.5 index=3', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")

; Run this set of numbers if IP Address = 10.1.2.*
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.2.5 index=1', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.2 index=2', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.5 index=3', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")

Share this post


Link to post
Share on other sites



Wow, didn't think this one would be that difficult? I think I read somewhere that the only way to use so called wildcards is with stringregexp but I need help with the syntax.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Not the best code, but it should give you an idea.... using StringSplit to separate the octets of the IP address

StringSplit($IP, ".")
If @error Then Exit

If $IP[3] = 1 Or $IP[3] = 3 OR $IP[3] = 4 Or $IP[3] = 5 Then
;do stuff
ElseIf $IP[3] = 2 Then
;do stuff
EndIf

Edit: I always forget the Then keyword with AutoIt's If statements!

Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

I am not sure if I understand your predicament. So sorry if I am way out with the following.

In order to look for the ip ranges, you could do something like this:

#include <GUIConstants.au3>
#include <misc.au3>
Global $IP = @IPAddress1
;Create a new IP address with only the 7 leading characters
$newip = Stringleft(@IPAddress1,7)

;Run this set of numbers if IP Address = 10.1.2.*
if $newip = "10.2.1." Then
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.2.5 index=1', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.2 index=2', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.5 index=3', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")
Else
; Run this set of numbers for all other addresses
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.2 index=1', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.5 index=2', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")
Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.2.5 index=3', "")
WinWait("C:\WINDOWS\system32\netsh.exe")
WinWaitClose("C:\WINDOWS\system32\netsh.exe")
EndIf

You could use "IF OR" statments to further eliminate any IP addresses that are not in the ranges you want to manipulate.

Hope it helps


CheersNobby

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hope this helps:

Local $IP = StringSplit(@IPAddress1, '.')
; $IP[1] will now give the first octet, etc.

If $IP[1] = 10 And $IP[2] = 1 Then
    If $IP[3] = 2 Then
        MsgBox(0x40, 'Information', 'Your IP address is 10.1.2.x.')
    ElseIf $IP[3] >= 1 And $IP[3] <= 5 Then
        MsgBox(0x40, 'Information', 'Your IP address is 10.1.[1/3/4/5].x.')
    Else
        MsgBox(0x10, 'Error', 'Your IP address is not 10.1.[1-5].x.')
    EndIf
Else
    MsgBox(0x10, 'Error', 'Your IP address is not 10.1.x.x.')
EndIf

Edit: Optimised the above code a little further so the remainder of this message no longer applies.

For anyone who's interested, these two lines are identical:

If $IP[3] = 1 Or $IP[3] = 3 Or $IP[3] = 4 Or $IP[3] = 5 Then
If StringInStr('-1-3-4-5-', '-' & $IP[3] & '-') Then

I personally find the second one tidier.

Edited by LxP

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Okay, I used a little from here and there and came up with this. Does it look correct?

#include <GUIConstants.au3>

If IsAdmin() = 0  Then
        RunAsSet("username", "domain", "Password", 0)
EndIf
        Local $IP = StringSplit(@IPAddress1, '.')
    ; $IP[1] will now give the first octet, etc.

    If $IP[1] = 10 And $IP[2] = 1 Then
        If $IP[3] = 2 Then
            SplashTextOn('DNS Configuration', @LF & 'Please wait while your' & @LF & 'DNS numbers are configured...', 275, 75)
            Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.2.5 index=1', "", @SW_HIDE)
            WinWait("C:\WINDOWS\system32\netsh.exe")
            WinWaitClose("C:\WINDOWS\system32\netsh.exe")
            Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.2 index=2', "", @SW_HIDE)
            WinWait("C:\WINDOWS\system32\netsh.exe")
            WinWaitClose("C:\WINDOWS\system32\netsh.exe")
            Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.5 index=3', "", @SW_HIDE)
            WinWait("C:\WINDOWS\system32\netsh.exe")
            WinWaitClose("C:\WINDOWS\system32\netsh.exe")
            SplashOff()
        ElseIf $IP[3] = 1 Or $IP[3] = 3 OR $IP[3] = 4 Or $IP[3] = 5 Then
            SplashTextOn('DNS Configuration', @LF & 'Please wait while your' & @LF & 'DNS numbers are configured...', 275, 75)
            Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.2 index=1', "", @SW_HIDE)
            WinWait("C:\WINDOWS\system32\netsh.exe")
            WinWaitClose("C:\WINDOWS\system32\netsh.exe")
            Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.1.5 index=2', "", @SW_HIDE)
            WinWait("C:\WINDOWS\system32\netsh.exe")
            WinWaitClose("C:\WINDOWS\system32\netsh.exe")
            Run('netsh interface ip add dns name="Local Area Connection" addr=10.1.2.5 index=3', "", @SW_HIDE)
            WinWait("C:\WINDOWS\system32\netsh.exe")
            WinWaitClose("C:\WINDOWS\system32\netsh.exe")
            SplashOff()
        Else
            Exit
        EndIf
    Else
        Exit
    EndIf
Edited by big_daddy

Share this post


Link to post
Share on other sites

Okay, I have a small problem now. When running this as a normal user it will not set the DNS numbers. So I tried to use the RunAsSet() and now I get an error that says:

Error: Unable to execute the external program.

The directory name is invalid.

It is showing the first run() command as the line with the error.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Okay, I have a small problem now. When running this as a normal user it will not set the DNS numbers. So I tried to use the RunAsSet() and now I get an error that says:

Error: Unable to execute the external program.

The directory name is invalid.

It is showing the first run() command as the line with the error.

Is your script running from the local disk available to all users ?

If not make sure you specify the workingdir on the RunWait command !

Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Is your script running from the local disk available to all users ?

If not make sure you specify the workingdir on the RunWait command !

The script is running from a network share available to all users, but the program it is trying to run is an internal windows command. So what would I specify as the workingdir?

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

The script is running from a network share available to all users, but the program it is trying to run is an internal windows command. So what would I specify as the workingdir?

Any available directory will do.... I use @tempDir when the program doesn't require a specific one.. Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Any available directory will do.... I use @tempDir when the program doesn't require a specific one..

Thanks a lot man! I was interpreting the workingdir as something else. Now I know how to fix errors in several other scripts I have tried to make.

Share this post


Link to post
Share on other sites

Thanks a lot man! I was interpreting the workingdir as something else. Now I know how to fix errors in several other scripts I have tried to make.

This is a common issue, maybe we should put a remark about it in the helpfile .....


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Here's a simplified version of your code if you're interested:

#Include <GUIConstants.au3>

If Not IsAdmin() Then RunAsSet('Username', 'Domain', 'Password', 0)

Local $IP = StringSplit(@IPAddress1, '.')
; $IP[1] will now give the first octet, etc.

Local $DNS
If $IP[1] = 10 And $IP[2] = 1 Then
    If $IP[3] = 2 Then
        $DNS = '10.1.2.5/10.1.1.2/10.1.1.5'
    ; If this next line is tested then that means that $IP[3] does NOT equal 2
    ; (Because if it did, we would not reach this point)
    ; Therefore we can test if it's between 1 and 5 and know that it's 1/3/4/5
    ; This makes the code simpler
    ElseIf $IP[3] >= 1 And $IP[3] <= 5 Then
        $DNS = '10.1.2.5/10.1.1.2/10.1.1.5'
    EndIf
    If $DNS <> '' Then
        SplashTextOn('DNS Configuration', @LF & 'Please wait while your' & @LF & 'DNS numbers are configured...', 275, 85)
        $DNS = StringSplit($DNS, '/')
        ; $DNS[1] will now give the first address, etc.
        ; $DNS[0] gives the number of DNS addresses supplied
        ; This means that we can use a loop to go through them
        For $I = 1 To $DNS[0]
            RunWait('netsh interface ip add dns name="Local Area Connection" addr=' & $DNS[$I] & ' index=' & $I, @WorkingDir, @SW_HIDE)
        Next
    EndIf
EndIf

Share this post


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
Sign in to follow this  
Followers 0