Author Posts

August 12, 2013 at 5:09 am

In the perpetual pursuit of being the laziest admin on the planet I am creating a de-provisioning script for servers when we retire them. One of the things I am trying to automate is the cleanup of DNS of any statically assigned records (A, CNAME, MX, etc...)

Example:
To get the info needed about a server name:

dnscmd $DomainContoller /enumrecords contoso.com $ServerName

Now I need a way to read the ip of: 10.20.30.41 and translate it to the zone of

20.10.in-addr.arpa.

for an output like this:

41.20.10.in-addr.aroa.

This is way over my feeble programming brain. any input will be met with the happy dance....

Mike

August 12, 2013 at 8:22 am

Something like this should do:

PS C:\> $ipAddress = '10.20.30.41'
PS C:\> $ipAddress -replace '^(\d+)\.(\d+)\.\d+\.(\d+)$','$3.$2.$1.in-addr.arpa'
41.20.10.in-addr.arpa

Brief explanation:
This uses a regular expression search and replace. It breaks the IP address into 4 parts (the 4 integer values, discovered by \d+, which means one or more digits, separated by \., which means '.' but needs to be escaped by backslash because '.' has special meaning in regular expressions), and captures the first, second and last parts that it discovers (capture is done by enclosure in round brackets). Once it has captured those values, it replaces the entire string with a new string, using the 3rd, 2nd, and 1st captured values separated by periods, followed by .in-addr.arpa.

August 12, 2013 at 11:13 am

Ouch. Now I gots dain brammage....

It works very well. Now I am off to fish...

Thanks for the solution AND the method.

Mike

August 12, 2013 at 11:43 am

Mike,

The solution I provided is nice in terms of being short and to the point, but regular expressions are not intuitive and come with a steep learning curve.

Here's another (regular expression-free) solution that will likely help you solve problems like this a little more easily on your own as you do more with PowerShell:

# Start with an IP address
$ipAddress = '10.20.30.41'

# Now lets split the address into its parts by using the dot ('.')
$ipAddressParts = $ipAddress.Split('.')

# Now reverse the array in place
[array]::Reverse($ipAddressParts)

# For the zone, we only care about the first 2 and last 1 part of the ip address, so lets get those
$importantIpAddressParts = $ipAddressParts | Select-Object -First 2 -Last 1

# Now that we have those parts, lets join them with '.' and add the zone suffix
$zone = [string]::Join('.',$importantIpAddressParts) + '.in-addr.arpa'

# Now output the zone
$zone

This is a good example that shows the power of regular expressions though, because what I do here in many lines of script I can accomplish in a single line with a regular expression like I did earlier. Granted, I could have made this approach shorter by combining some of the steps, but I wanted to flush it out so that you could see what is being done each step of the way.

August 13, 2013 at 6:41 pm

Or further simplified, after splitting the array just use a string format or string concantenation...


# Now lets split the address into its parts by using the dot (‘.’)
$ipAddressParts = $ipAddress.Split('.')

# Use String Format....
"{0}.{1}.{2}.in-addr.arpa" -f $ipAddressParts[3], $ipAddressParts[2], $ipAddressParts[0]
# Or other string concantenation
$ipAddressParts[3] + "." + $ipAddressParts[2] + "." + $ipAddressParts[0] + ".in-addr.arpa"