これが古いものであることは知っていますが、それでも貢献したいと思っています。「NsLookup Hostname Server」からのシェル出力を使用して、ドメイン内のコンピューター名から IPv4 アドレスを取得し、DNS サーバー/ipv6 アドレスなどの他の情報を取り除きました..
これは簡単に実行できますが、機能します。シェルが失敗した場合は、C# の組み込みの nslookup メソッドを使用するフェイルオーバーも追加されます。
かなり長いですが、外部ライブラリを使用せずに、または組み込みの nslookup 関数を使用せずにシェルから ipv4 を読み取る可能性を与えてくれました。これにより、dns サーバーを選択できるようになります。
途中の if ループについて疑問がある場合は、もっと洗練された解決策があるかもしれませんが、私の個人的な使用では、これは非常にうまくいきました。ドメイン内のほとんどのホストは 2 つの ipv6 と 2 つの ipv4 を返したため、最大 4 回テストされます。 .
これが役立つことを願っています..
private void button1_Click(object sender, EventArgs e)
{
IPAddress[] ips = NsLookup(computername, dnsserver);
txtResult.Text = string.Empty;
if (ips != null)
{
txtResult.Text = ips[0].ToString();
txtResult.Text += Environment.NewLine;
if (ips[1] != null)
{
txtResult.Text += ips[1].ToString();
}
else
{
}
}
else
{
txtResult.Text = "No IP found";
}
}
public IPAddress[] NsLookup(string computername, string domaincontroller)
{
IPAddress[] ips = new IPAddress[2];
try
{
// Creating streamreaders to read the output and the errors
StreamReader outputReader = null;
StreamReader errorReader = null;
string nslookup = @"C:\Windows\System32\Nslookup.exe";
try
{
// Setting process startupinfo
ProcessStartInfo processStartInfo = new ProcessStartInfo(nslookup, computername + " " + domaincontroller);
processStartInfo.ErrorDialog = false;
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Minimized;
// Starting Process
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
if (processStarted)
{
// Catching the output streams
outputReader = process.StandardOutput;
errorReader = process.StandardError;
string errorresult = errorReader.ReadLine();
errorReader.Close();
if (errorresult != null)
{
// Failure got thrown in NsLookup Streamreading, try build-in Method
try
{
ips = Dns.GetHostAddresses(computername);
return ips;
}
catch
{
return null;
}
}
else
{
// Clearing out all the values before the addresses.
outputReader.ReadLine();
outputReader.ReadLine();
outputReader.ReadLine();
outputReader.ReadLine();
// Reading and Verifying the first outputline (the address is found after "Addresses: ") - 2 part of the array is taken (after second space)
string outputline = outputReader.ReadLine();
string[] outputlineaftersplit = outputline.Split(' ');
string ipfortesting = outputlineaftersplit[2].Trim();
if (verifyIP(ipfortesting) != null) // First entry is ipv4
{
ips[0] = verifyIP(ipfortesting);
outputline = outputReader.ReadLine();
ipfortesting = outputline.Trim();
if (verifyIP(ipfortesting) != null) // First and second entry are ipv4
{
ips[1] = verifyIP(ipfortesting);
return ips;
}
else
{
return ips;
}
}
else
{
outputline = outputReader.ReadLine();
ipfortesting = outputline.Trim();
if (verifyIP(ipfortesting) != null)
{
ips[0] = verifyIP(ipfortesting);
outputline = outputReader.ReadLine();
ipfortesting = outputline.Trim();
if (verifyIP(ipfortesting) != null)
{
ips[0] = verifyIP(ipfortesting);
outputline = outputReader.ReadLine();
ipfortesting = outputline.Trim();
if (verifyIP(ipfortesting) != null)
{
ips[1] = verifyIP(ipfortesting);
return ips;
}
else
{
return ips;
}
}
else
{
return ips;
}
}
else
{
outputline = outputReader.ReadLine();
ipfortesting = outputline.Trim();
if (verifyIP(ipfortesting) != null)
{
ips[0] = verifyIP(ipfortesting);
outputline = outputReader.ReadToEnd();
ipfortesting = outputline.Trim();
if (verifyIP(ipfortesting) != null)
{
ips[1] = verifyIP(ipfortesting);
return ips;
}
else
{
return ips;
}
}
else
{
ips = null;
return ips;
}
}
}
}
}
else
{
// Failure got thrown in NsLookup Streamreading, try build-in Method
try
{
ips = Dns.GetHostAddresses(computername);
return ips;
}
catch
{
return null;
}
}
}
catch
{
System.Windows.Forms.MessageBox.Show("ERROR 1");
// Failure got thrown in NsLookup Streamreading, try build-in Method
try
{
ips = Dns.GetHostAddresses(computername);
return ips;
}
catch
{
return null;
}
}
finally
{
if (outputReader != null)
{
outputReader.Close();
}
}
}
catch
{
System.Windows.Forms.MessageBox.Show("ERROR 2");
// Failure got thrown in NsLookup Streamreading, try build-in Method
try
{
ips = Dns.GetHostAddresses(computername);
return ips;
}
catch
{
return null;
}
}
}
public IPAddress verifyIP(string ipfromreader)
{
IPAddress ipresult = null;
bool isIP = IPAddress.TryParse(ipfromreader, out ipresult);
if (isIP && (ipresult.AddressFamily != AddressFamily.InterNetworkV6))
{
return ipresult;
}
else
{
return null;
}
}
}
}