Finding the DNS suffix...

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Finding the DNS suffix...

#1 Post by Merlin » 03 Jul 2017 07:52

Hi everyone,



I've spent an inordinate amount of time on what I'm sure should be a fairly trivial matter...

If I run an IPconfig on my target machine, the line I'm interested in is this:

Connection-specific DNS Suffix . : xx-xxxx-xx.compute.internal


NB! x can vary in length, i.e.: xx-xxxxxxx-xx

I've dumbed all of my attempts right down, to the points where I'm now just doing a simple Findstr:

Code: Select all

ipconfig | findstr /e /c:"compute.internal"


All I need is the X section.

Any input would be greatly appreciated.

Thank you.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Finding the DNS suffix...

#2 Post by ShadowThief » 03 Jul 2017 08:00

Does the x part contain any periods?

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: Finding the DNS suffix...

#3 Post by Merlin » 03 Jul 2017 08:04

ShadowThief wrote:Does the x part contain any periods?


Hi Shadow,



No periods, just hyphens.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Finding the DNS suffix...

#4 Post by aGerman » 03 Jul 2017 11:00

That line might be working for you:

Code: Select all

for /f "tokens=1* delims=:" %%i in ('ipconfig^|findstr /ec:"compute.internal"') do for /f "delims=. " %%k in ("%%j") do set "var=%%k"

Variable %var% should contain what you're looking for.

Steffen

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Finding the DNS suffix...

#5 Post by Compo » 03 Jul 2017 12:28

If you could determine the search suffix first, perhaps via the registry, then your script might be more useful:

Code: Select all

@Echo Off
Set "HSCs=HKLM\SYSTEM\CurrentControlSet\services"

Set "Sfx="
For /F "EOL=H Tokens=2*" %%A In (
   'Reg Query "%HSCs%\Tcpip\Parameters" /V SearchList') Do Set "Sfx=%%~B"
If Not Defined Sfx Exit/B

Set "Var="
For /F "Tokens=2 Delims=:" %%A In ('IPConfig^|FindStr/ELIC:".%Sfx%"'
) Do If Not "%%A"==" " Set "Var=%%A" & Call Set "Var=%%Var:.%Sfx%=%%"
If Defined Var Echo%Var%
Pause


You'd need to check if that particular registry key and data is correct for this task though.

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: Finding the DNS suffix...

#6 Post by Merlin » 04 Jul 2017 00:57

aGerman,

Lovely stuff! :D Thank you.



Compo,

If I understand correctly, your script is scanning the registry for the suffix and then outputting that into a var? I wasn't able to pick up the suffix with your script, but I would like to get it working. It's a whole new concept to me; picking up the info' from the registry via batch.



Thanks for your help, everyone.

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: Finding the DNS suffix...

#7 Post by Merlin » 07 Jul 2017 03:35

aGerman wrote:That line might be working for you:

Code: Select all

for /f "tokens=1* delims=:" %%i in ('ipconfig^|findstr /ec:"compute.internal"') do for /f "delims=. " %%k in ("%%j") do set "var=%%k"

Variable %var% should contain what you're looking for.

Steffen


Hi Steffen,



This line worked perfectly on my test box, however, I now have it running on our live environment and I've picked up a problem. The live environment is running a domain and that's causing a problem with the script. It no longer detects the suffix correctly.

If I run an ipconfig /all, I can still see the suffix that I require, prefixed to compute.internal.

I suspect that I need to make use usebackq, but am still struggling. Your input would be greatly appreciated.

This is what I have at present:

Code: Select all

For /f "usebackq tokens=1* delims=:" %%i in (`ipconfig /all^|FindStr /ec:"compute.internal"`) Do For /f "delims=. " %%k in ("%%j") Do Set "Region=%%k"


Image

Thank you.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Finding the DNS suffix...

#8 Post by aGerman » 08 Jul 2017 13:17

I didn't test it but I assume this code should work in both cases.

Code: Select all

for /f "tokens=1* delims=:" %%i in ('ipconfig^|findstr /ec:"compute.internal"') do (
  if "%%j"=="" (
    for /f "delims=. " %%k in ("%%i") do set "var=%%k"
  ) else (
    for /f "delims=. " %%k in ("%%j") do set "var=%%k"
  )
)

Steffen

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: Finding the DNS suffix...

#9 Post by Merlin » 10 Jul 2017 01:57

aGerman wrote:I didn't test it but I assume this code should work in both cases.

Code: Select all

for /f "tokens=1* delims=:" %%i in ('ipconfig^|findstr /ec:"compute.internal"') do (
  if "%%j"=="" (
    for /f "delims=. " %%k in ("%%i") do set "var=%%k"
  ) else (
    for /f "delims=. " %%k in ("%%j") do set "var=%%k"
  )
)

Steffen


Hi Steffen,



Many thanks for the input. Every time I think I'm getting the hang of batch, it catches me out. Your code is significantly different to what I was playing around with.

I wasn't able to detect the suffix using your code, however, with a little modification, it is not working as required...

Code: Select all

for /f "tokens=1* delims=:" %%a in ('ipconfig /all^|findstr /ec:"compute.internal"') do (for /f "delims=. " %%b in ("%%a") do set "Region=%%b")


Many thanks for your help. It's been a great aid. :)



Kind regards,

Nic

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Finding the DNS suffix...

#10 Post by Compo » 10 Jul 2017 04:16

Merlin wrote:Compo,

If I understand correctly, your script is scanning the registry for the suffix and then outputting that into a var? I wasn't able to pick up the suffix with your script, but I would like to get it working. It's a whole new concept to me; picking up the info' from the registry via batch.


You have not indicated whether those settings are held in that key and it's an issue with the coding or otherwise.

Additionally, what is output when you enter the following in a cmd window:

Code: Select all

For /F "Skip=1 Delims={}" %A In ('WMIC NICConfig Where "IPEnabled='TRUE'" Get DNSDomainSuffixSearchOrder') Do @For %B In (%A) Do @Echo( [%B]

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: Finding the DNS suffix...

#11 Post by Merlin » 10 Jul 2017 04:30

Compo wrote:
Merlin wrote:Compo,

If I understand correctly, your script is scanning the registry for the suffix and then outputting that into a var? I wasn't able to pick up the suffix with your script, but I would like to get it working. It's a whole new concept to me; picking up the info' from the registry via batch.


You have not indicated whether those settings are held in that key and it's an issue with the coding or otherwise.

Image

Additionally, what is output when you enter the following in a cmd window:

Code: Select all

For /F "Skip=1 Delims={}" %A In ('WMIC NICConfig Where "IPEnabled='TRUE'" Get DNSDomainSuffixSearchOrder') Do @For %B In (%A) Do @Echo( [%B]


["eu-west-1.ec2-utilities.amazonaws.com"]
["us-east-1.ec2-utilities.amazonaws.com"]
["ec2-utilities.amazonaws.com"]
["ec2.internal"]
["compute-1.internal"]
["eu-west-1.compute.internal"]
["XXXHIDDENXXX.XX.XX"]

The script is working at this point. :)

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Finding the DNS suffix...

#12 Post by Compo » 10 Jul 2017 12:03

Merlin, might I suggest this then to retrieve the part of the suffix you need.

Code: Select all

@Echo Off
For /F "Skip=1 Delims={}" %%A In (
   'WMIC NICConfig Where "IPEnabled='TRUE'" Get DNSDomainSuffixSearchOrder'
) Do For %%B In (%%A) Do If /I "%%~xB"==".internal" For %%C In (%%~nB
) Do If /I "%%~xC"==".compute" Echo( [%%~nC]
Pause

Merlin
Posts: 14
Joined: 11 May 2017 07:07
Location: Cape Town, ZA
Contact:

Re: Finding the DNS suffix...

#13 Post by Merlin » 11 Jul 2017 02:04

Compo wrote:Merlin, might I suggest this then to retrieve the part of the suffix you need.

Code: Select all

@Echo Off
For /F "Skip=1 Delims={}" %%A In (
   'WMIC NICConfig Where "IPEnabled='TRUE'" Get DNSDomainSuffixSearchOrder'
) Do For %%B In (%%A) Do If /I "%%~xB"==".internal" For %%C In (%%~nB
) Do If /I "%%~xC"==".compute" Echo( [%%~nC]
Pause


Nifty, Compo. :D

It outputs:

[eu-west-1]


That'll take me a while to break down, but it's a neat trick for sure.

Thanks.

Post Reply