Skip to content

Instantly share code, notes, and snippets.

@cleydson
Created May 23, 2019 13:26
Show Gist options
  • Save cleydson/7251cd18316cf23770413197fa904b64 to your computer and use it in GitHub Desktop.
Save cleydson/7251cd18316cf23770413197fa904b64 to your computer and use it in GitHub Desktop.
/*
Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices.ActiveDirectory;
using System.Threading;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;
using System.Security.Authentication;
using System.Security.AccessControl;
using System.Security.Principal;
namespace DirectoryServicePortTest
{
class Program
{
private static List<int> _tcpPorts;
private static List<int> _udpPorts;
private static string _domain = "";
private static IPAddress _ipAddr = null;
static void Main(string[] args)
{
if (ParseArgs(args))
{
try
{
if (_domain.Length > 0)
{
try
{
TestForestFunctionalLevel();
TestDomainFunctionalLevel();
}
catch (ActiveDirectoryObjectNotFoundException)
{
Console.WriteLine("The domain {0} could not be found.\n", _domain);
}
}
if (null != _ipAddr)
{
if (_tcpPorts.Count > 0)
{
TestTcpPorts(_tcpPorts);
}
if (_udpPorts.Count > 0)
{
TestUdpPorts(_udpPorts);
}
}
}
catch (AuthenticationException ex)
{
Console.WriteLine(ex.Message);
}
}
else
{
PrintUsage();
}
Console.Write("Press <enter> to continue.");
Console.ReadLine();
}
static void PrintUsage()
{
string currentApp = Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Console.WriteLine("Usage: {0} \n-d <domain> \n-ip \"<server IP address>\" \n[-tcp \"<tcp_port1>,<tcp_port2>,etc\"] \n[-udp \"<udp_port1>,<udp_port2>,etc\"]", currentApp);
}
static bool ParseArgs(string[] args)
{
bool fReturn = false;
string ipAddress = "";
try
{
_tcpPorts = new List<int>();
_udpPorts = new List<int>();
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if ("-tcp" == arg | "/tcp" == arg)
{
i++;
string portList = args[i];
_tcpPorts = ParsePortList(portList);
}
if ("-udp" == arg | "/udp" == arg)
{
i++;
string portList = args[i];
_udpPorts = ParsePortList(portList);
}
if ("-d" == arg | "/d" == arg)
{
i++;
_domain = args[i];
}
if ("-ip" == arg | "/ip" == arg)
{
i++;
ipAddress = args[i];
}
}
}
catch (ArgumentOutOfRangeException)
{
return false;
}
if (_domain.Length > 0 || ipAddress.Length > 0)
{
fReturn = true;
}
if (ipAddress.Length > 0)
{
_ipAddr = IPAddress.Parse(ipAddress);
}
return fReturn;
}
static List<int> ParsePortList(string portList)
{
List<int> ports = new List<int>();
char[] separators = {',', ';', ':'};
string[] portStrings = portList.Split(separators);
foreach (string portString in portStrings)
{
try
{
ports.Add(Convert.ToInt32(portString));
}
catch (FormatException)
{
}
}
return ports;
}
static void TestForestFunctionalLevel()
{
Console.WriteLine("Testing forest functional level.");
DirectoryContext dirContext = new DirectoryContext(DirectoryContextType.Forest, _domain, null, null);
Forest forestContext = Forest.GetForest(dirContext);
Console.Write("Forest Functional Level = {0} : ", forestContext.ForestMode);
if (forestContext.ForestMode >= ForestMode.Windows2003Forest)
{
Console.WriteLine("PASSED");
}
else
{
Console.WriteLine("FAILED");
}
Console.WriteLine();
}
static void TestDomainFunctionalLevel()
{
Console.WriteLine("Testing domain functional level.");
DirectoryContext dirContext = new DirectoryContext(DirectoryContextType.Domain, _domain, null, null);
Domain domainObject = Domain.GetDomain(dirContext);
Console.Write("Domain Functional Level = {0} : ", domainObject.DomainMode);
if (domainObject.DomainMode >= DomainMode.Windows2003Domain)
{
Console.WriteLine("PASSED");
}
else
{
Console.WriteLine("FAILED");
}
Console.WriteLine();
}
static List<int> TestTcpPorts(List<int> portList)
{
Console.WriteLine("Testing TCP ports to {0}:", _ipAddr.ToString());
List<int> failedPorts = new List<int>();
foreach (int port in portList)
{
Console.Write("Checking TCP port {0}: ", port);
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect(_ipAddr, port);
tcpClient.Close();
Console.WriteLine("PASSED");
}
catch (SocketException)
{
failedPorts.Add(port);
Console.WriteLine("FAILED");
}
}
Console.WriteLine();
return failedPorts;
}
static List<int> TestUdpPorts(List<int> portList)
{
Console.WriteLine("Testing UDP ports to {0}:", _ipAddr.ToString());
List<int> failedPorts = new List<int>();
foreach (int port in portList)
{
Console.Write("Checking UDP port {0}: ", port);
UdpClient udpClient = new UdpClient();
try
{
udpClient.Connect(_ipAddr, port);
udpClient.Close();
Console.WriteLine("PASSED");
}
catch (SocketException)
{
failedPorts.Add(port);
Console.WriteLine("FAILED");
}
}
Console.WriteLine();
return failedPorts;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment