Skip to content

Instantly share code, notes, and snippets.

@h4r7w3l1
Last active October 27, 2023 14:27
Show Gist options
  • Save h4r7w3l1/23fcca5f7b21ed3709d8e138966ffdf3 to your computer and use it in GitHub Desktop.
Save h4r7w3l1/23fcca5f7b21ed3709d8e138966ffdf3 to your computer and use it in GitHub Desktop.
whois_cli.csx

whois_cli

Implementation of whois writen on dotnet-scripts

Make sure you have installed dotnet tool for running .csx extensions

  • Try in cli:
dotnet script https://gist.githubusercontent.com/h4r7w3l1/23fcca5f7b21ed3709d8e138966ffdf3/raw/6df94156a4a2d91f6d81b59615717f3444341539/whois_cli.csx "yandex.ru" "rambler.ru" "notreallyexistedfuckingdomain.com"
#!/usr/bin/env dotnet-script
using System;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
Parallel.ForEach(Args, (domain) =>
{
string normalizedDomain = domain.ToLower().Any(v => !"abcdefghijklmnopqrstuvdxyz0123456789.-".Contains(v))
? new System.Globalization.IdnMapping().GetAscii(domain)
: domain;
string[] domainTld = domain.Split('.');
if (domainTld.Length < 2)
{
Console.WriteLine("Invalid domain format");
return;
}
string whoisServer = $"{domainTld[1]}.whois-servers.net";
string result = string.Empty;
using (TcpClient tcpClient = new TcpClient())
{
try
{
tcpClient.Connect(whoisServer.Trim(), 43);
byte[] domainQueryBytes = Encoding.ASCII.GetBytes(normalizedDomain + "\r\n");
using (Stream stream = tcpClient.GetStream())
{
stream.Write(domainQueryBytes, 0, domainQueryBytes.Length);
using StreamReader sr = new StreamReader(tcpClient.GetStream(), Encoding.UTF8);
StringBuilder stringBuilder = new StringBuilder();
string row;
while ((row = sr.ReadLine()) != null)
{
if (row.StartsWith("#") || row.StartsWith("%") || string.IsNullOrWhiteSpace(row.Trim()))
{
continue;
}
if (row.StartsWith(">>>") || row.StartsWith("Last updated on"))
{
break;
}
stringBuilder.AppendLine(row.Trim());
}
result = stringBuilder.ToString();
}
}
catch (Exception ex)
{
Console.WriteLine(">> Whois: " + domain + "\nError: " + ex.Message);
return;
}
}
Console.WriteLine(">> Whois: " + domain + "\n" + result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment