Wednesday, April 8, 2009

NSLOOKUP and POWERSHELL

# FORWARD DNS RESOLUTON WITH NSLOOKUP
# This function calls nslookup (which is a standard microsoft tool)
# and fetches from the output string just the IP address.
# You can specify a Name Server to use.
# Please note the $Comp can be an array, in which case you should
# run into it with foreach().
# ==========================================
Function forward_dns
{
$cmd = "nslookup " + $args[0] + " " + $ns
$result = Invoke-Expression ($cmd)
trap
{
$global:controlladns = $true
$global:solved_ip = "No record found"
continue
}
$global:controlladns = $false
$global:solved_ip = $result.SyncRoot[4]
if (-not $global:controlladns)
{
$leng = $global:solved_ip.Length -10
$global:solved_ip =
$global:solved_ip.substring(10,$leng)
}
}
$comp = "hostname" # Hostname to resolve to IP
$ns = "DNS name" # Name Server which will do name resolution
forward_dns $comp
echo "Host: $comp - IP : $global:solved_ip - NS: $ns"
# ==========================================


# REVERSE DNS RESOLUTON WITH NSLOOKUP
# This function calls nslookup (which is a standard microsoft tool)
# and fetches from the output string just the hostname.
# You can specify a Name Server to use.
# Please note the $Comp can be an array, in which case you should
# run into it with foreach().
# ==========================================
Function reverse_dns
{
$cmd2 = "nslookup " + $args[0] + " " + $ns
$result2 = Invoke-Expression ($cmd2)
$global:reverse_solved_ip = $result2.SyncRoot[3]
if ($result2.count -lt 4) # Integrity check
{
$global:reverse_solved_ip = "No record found"
}
else
{
$leng2 = $global:reverse_solved_ip.length - 9
$global:reverse_solved_ip =
$global:reverse_solved_ip.substring(9,$leng2)
}
}
$comp = "X.X.X.X" # Hostname to resolve to IP
$ns = "DNS name" # Name Server which will do name resolution
reverse_dns $comp
echo "IP: $comp - Host : $global:reverse_solved_ip - NS: $ns"
# ==========================================

# ==========================================
# Don't use DIG or get-dns.
# You don't need to install any add-on just for name resolution.
# Remember to open PORT 53 on you firewall for DNS requests.
# ==========================================

# ==========================================
# Note also the use of the ".substring" method.
# Example:
# $a="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# $a = $a.substring(0,3)
# When you run this command and then echo back the value of $a you should
get the following:
# ABC
# ==========================================

9 comments:

  1. Thanks, it works like a charm :)

    Regards from Serbia

    ReplyDelete
  2. thanks, solve the Problem with output redirection of nslookup

    regards

    ReplyDelete
  3. Re: forward_dns
    Thanks for the code, worked a treat until I found it resolving IPV6 addresses where I was expecting (and wanting!) IPV4.
    Tested against both Active Directory and BIND DNS and the following worked for me.
    I added in type=a to solve my issue. Just sharing incase other have the same problem :)

    $cmd = "nslookup " + "-type=a " + $args[0] + " " + $ns

    ReplyDelete
  4. why not just use?

    [System.Net.Dns]::GetHostEntry($IP)
    [System.Net.Dns]::GetHostAddresses($ServerName)

    ReplyDelete
    Replies
    1. Because the .Net class doesn't allow specifying the DNS server. You may be interested in testing a specific server and the above scripts allow this.

      Delete
  5. Any idea why this happens:

    PS C:\Users\Administrator> forward_dns $comp
    nslookup : Non-authoritative answer:
    At line:1 char:1
    + nslookup -type=any microsoft.com 192.168.1.1
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (Non-authoritative answer::String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

    ReplyDelete
  6. what if i want to use a list of servers in a txt file

    ReplyDelete
    Replies
    1. Get-Content "textfile"

      This will return an array of strings with each line as a entry in the array

      Delete