-
-
Save vnisor/940500b7e09187d92ce1 to your computer and use it in GitHub Desktop.
PowerShell Whois Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author: Oğuzhan YILMAZ | |
// Web : www.oguzhan.info | |
// Email : aspsrc@gmail.com | |
// 12/13/2011 | |
namespace PoshWhois | |
{ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Management.Automation; | |
using System.Net.Sockets; | |
using System.Text; | |
[Cmdlet(VerbsCommon.Get, "Whois")] | |
public class WhoisCommand : Cmdlet | |
{ | |
[Parameter(Position=0, Mandatory= true)] | |
public string domain { get; set; } | |
protected override void ProcessRecord() | |
{ | |
if (String.IsNullOrEmpty(domain)) | |
{ | |
WriteObject("Domain name required"); | |
return; | |
} | |
var _whoisInfo = GetWhois(domain); | |
WriteObject(_whoisInfo); | |
} | |
// http://dotnet-snippets.com/dns/gets-the-whois-information-SID581.aspx | |
private string GetWhois(string _name) | |
{ | |
var _host = WhoisHost(_name); | |
try | |
{ | |
StringBuilder stringBuilderResult = new StringBuilder(); | |
TcpClient tcpClinetWhois = new TcpClient(_host, 43); | |
NetworkStream networkStreamWhois = tcpClinetWhois.GetStream(); | |
BufferedStream bufferedStreamWhois = new BufferedStream(networkStreamWhois); | |
StreamWriter streamWriter = new StreamWriter(bufferedStreamWhois); | |
streamWriter.WriteLine(_name); | |
streamWriter.Flush(); | |
StreamReader streamReaderReceive = new StreamReader(bufferedStreamWhois); | |
while (!streamReaderReceive.EndOfStream) | |
stringBuilderResult.AppendLine(streamReaderReceive.ReadLine()); | |
return stringBuilderResult.ToString(); | |
} | |
catch (Exception ex) | |
{ | |
return String.Format("Error: {0}", ex.Message); | |
} | |
} | |
private string WhoisHost(string _name) | |
{ | |
_name = _name ?? String.Empty; | |
var _extension = _name.Split('.').Last().ToUpper(); | |
var _list = new Dictionary<string, string>(); | |
_list.Add("COM", "whois.internic.net"); | |
_list.Add("NET", "whois.internic.net"); | |
_list.Add("ORG", "whois.publicinterestregistry.net"); | |
_list.Add("INFO", "whois.afilias.info"); | |
return _list.ContainsKey(_extension) ? | |
_list[_extension] : | |
_list.First().Value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment