Advertisement
f0rkB0mb

Nicos QnD Whois

Dec 17th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. namespace Whois
  2. {
  3.     #region Namespaces
  4.     using System;
  5.     using System.Collections.Generic;
  6.     using System.Text;
  7.     using System.Net;
  8.     using System.IO;
  9.     using System.Xml;
  10.     using System.Net.Sockets;
  11.     #endregion
  12.  
  13.     class NicosWhoisTool
  14.     {
  15.         private static FileInfo f;
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             Console.Title = "Nicos little Whois-Tool";
  20.             //path to the serverlist:
  21.             var xmlFile = AppDomain.CurrentDomain.BaseDirectory + @"\serverlist.xml";
  22.            
  23.             //string-array to store the servers:
  24.             string[] output = new string[1024];
  25.             //counter variable:
  26.             int z = 0;
  27.  
  28.             #region parsing the xml-file
  29.  
  30.             using (XmlReader reader = XmlReader.Create(File.OpenRead(xmlFile)))
  31.             {
  32.                 //reading the xml-file
  33.                 while (reader.Read())
  34.                 {
  35.                         //navigate to the <server> element
  36.                         reader.ReadToFollowing("server");
  37.                         //opening the first-attribute (host="..."):
  38.                         reader.MoveToFirstAttribute();
  39.                         //writing the host-value into the array:
  40.                         string host = reader.Value;
  41.                         output[z] = host; //you could write this also short: output[z] = reader.Value;
  42.  
  43.                         z++;
  44.                 }
  45.             }
  46.             #endregion
  47.  
  48.             Console.WriteLine("Please insert the host/ip that you wanna lookup");
  49.             Console.Write("Host/IP:> ");
  50.             string check = Console.ReadLine();
  51.  
  52.             for (int i = 0; i < output.Length; i++)
  53.             {
  54.                 if (null != output[i])
  55.                 {
  56.                     try
  57.                     {
  58.                         Console.WriteLine("Checking {0} on Server: {1}", check, output[i]);
  59.                         //check the adress in the whois-databases:
  60.                         Console.WriteLine(GetWhois(output[i], check));
  61.                     }
  62.                     catch (Exception ex)
  63.                     {
  64.                         Console.ForegroundColor = ConsoleColor.Red;
  65.                         Console.WriteLine("\tError:\n{0}", ex.Message);
  66.                         Console.ForegroundColor = ConsoleColor.Gray;
  67.                     }
  68.                 }
  69.                 else
  70.                     break;
  71.             }
  72.             Console.WriteLine("\n\t\tWhois-Request done!");
  73.             Console.ReadLine();
  74.         }
  75.  
  76.         /// <summary>
  77.         /// Gets the Whois-DB-Information.
  78.         /// </summary>
  79.         /// <param name="whoisSvr">The Whois-Server as <see cref="System.String"/>.</param>
  80.         /// <param name="requestUrl">The request Domainname as <see cref="System.String"/>.</param>
  81.         /// <returns>A <see cref="System.String"/> with all the responses from the Servers.</returns>
  82.         private static string GetWhois(string whoisSvr, string requestUrl)
  83.         {
  84.             StringBuilder strbRes = new StringBuilder();
  85.             TcpClient tcpClientWhois = new TcpClient(whoisSvr, 43); //create client on whois-port 43
  86.             NetworkStream nwStrWhois = tcpClientWhois.GetStream();
  87.             BufferedStream bufStrWhois = new BufferedStream(nwStrWhois);
  88.             StreamWriter streamWriter = new StreamWriter(bufStrWhois);
  89.  
  90.             streamWriter.WriteLine(requestUrl);
  91.             streamWriter.Flush();
  92.  
  93.             StreamReader streamReaderReceive = new StreamReader(bufStrWhois);
  94.  
  95.             while (!streamReaderReceive.EndOfStream)
  96.             {
  97.                 strbRes.AppendLine(streamReaderReceive.ReadLine());
  98.             }
  99.             return strbRes.ToString();
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement