# 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
# ==========================================
LEARN TO <pre>!!
ReplyDeleteThanks, it works like a charm :)
ReplyDeleteRegards from Serbia
thanks, solve the Problem with output redirection of nslookup
ReplyDeleteregards
Re: forward_dns
ReplyDeleteThanks 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
why not just use?
ReplyDelete[System.Net.Dns]::GetHostEntry($IP)
[System.Net.Dns]::GetHostAddresses($ServerName)
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.
DeleteAny idea why this happens:
ReplyDeletePS 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
what if i want to use a list of servers in a txt file
ReplyDeleteGet-Content "textfile"
DeleteThis will return an array of strings with each line as a entry in the array