JDP 發表於 2007-11-22 11:47:39

C# 簡易的 NSLookUp

using System;
using System.Net;
namespace nslookup
{
/// <summary>
///
/// <title>nslookup</title>
/// <description>
/// *nix simple nslookup clone for the Win32 platform (Console Application)
/// Does A DNS lookup by Host Name or IP. Host Name lookups can return
/// multiple IP Ranges.
/// </description>
/// <author>Doug Bell</author>
/// <version>1.0</version>
/// <date>March 23 2002</date>
///
/// </summary>
class NSLookup
{
/// <summary>
///
/// <description>Application entry point.</description>
/// <param name="args">Host Address, IP Address or -help command line
options</param>
/// <return>int Return code 0 for success -1 for failure or error</return>
///
/// </summary>

static int Main(string[] args)
{
//Make sure we were passed something, otherwise return help.
if (args.Length < 1 || args.Equals("-help"))
{
Console.WriteLine("Usage is: nslookup | | -help");
Console.WriteLine("nslookup foo.bar.com (Returns IP Address for Host Name)");
Console.WriteLine("nslookup 123.123.123.123 (Returns Host Name for Address)");
Console.WriteLine("nslookup -help (Returns this Help Message)");
return -1;
}
else
{
//We have something, try to look it up....
try
{
//The IP or Host Entry to lookup
IPHostEntry ipEntry;
//The IP Address Array. Holds an array of resolved Host Names.
IPAddress [] ipAddr;
//Value of alpha characters
char[] alpha = "aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ-".ToCharArray();
//If alpha characters exist we know we are doing a forward lookup
if (args.IndexOfAny(alpha) != -1)
{
ipEntry = Dns.GetHostByName (args);
ipAddr = ipEntry.AddressList;
Console.WriteLine("\nHost Name : " + args);
int i = 0;
int len = ipAddr.Length;
for (i = 0; i < len; i++)
{
Console.WriteLine ("Address {0} : {1} ", i, ipAddr.ToString ());
}
return 0;
}
//If no alpha characters exist we do a reverse lookup
else
{
ipEntry = Dns.Resolve(args);
Console.WriteLine("Address : " + args);
Console.WriteLine("Host Name : " + ipEntry.HostName);
return 0;
}
}
catch(System.Net.Sockets.SocketException se)
{
// The system had problems resolving the address passed
Console.WriteLine(se.Message.ToString());
return -1;
}
catch(System.FormatException fe)
{
// Non unicode chars were probably passed
Console.WriteLine(fe.Message.ToString());
return -1;
}
}
}//End Main(string[])
}//End NSLookup Class
}//End nslookup namespace

Reference: http://www.c-sharpcorner.com/UploadFile/DougBell/NSLookUpDB00112052005013753AM/NSLookUpDB001.aspx
頁: [1]
查看完整版本: C# 簡易的 NSLookUp