简体   繁体   中英

How to scan a range of IP addresses on a specific port

I'm trying to figure out how to scan a specific TCP port within a range of IPaddresses?

This is my code so far:

static void Main(string[] args)
{
    IPAddress beginIP = IPAddress.Parse("192.168.1.1");
    IPAddress EndIP = IPAddress.Parse("192.168.3.3");
    int PortNumber = 80;

    bool portStatu = scanPort(BeginIP,EndIP,PortNumber);
}

public bool scanPort(IPAddress beginIP,IPAddress endIP , int portNumber)
{
     //What should i write...
}

You could use the TcpClient Class to check your ports, and run this in parallel in an async / await pattern (suitable for IO bound operations) using ActionBlock Class in the TPL dataflow library.

You can generate your ips like this

public static List<IPAddress> GenerateIPAddressList(string fromIP, string toIp)
{
   var start = IPAddress.Parse(fromIP).GetAddressBytes();
   var end = IPAddress.Parse(toIp).GetAddressBytes();
   var addresses = new List<IPAddress>();
   for (var i = start[0]; i <= end[0]; i++)
      for (var j = start[1]; j <= end[1]; j++)
         for (var k = start[2]; k <= end[2]; k++)
            for (var l = start[2]; l <= end[3]; l++)
               addresses.Add(new IPAddress(new[] { i, j, k, l }));
   return addresses;
}

Demo here


To get this working in an async Parallel and IO suitable away, first create your worker method

Main Method

private static readonly List<Tuple<IPAddress,int>> _passed = new List<Tuple<IPAddress, int>>();
private static readonly List<Tuple<IPAddress, int, int>> _failed = new List<Tuple<IPAddress, int, int>>();
private static readonly object _sync = new object();

 ...

public static async Task TestSocket(IPAddress ip)
{
   try
   {
      Console.WriteLine("testing : " + ip);
      // create a socket
      using (var tcpClient = new TcpClient())
      {
         // connect in an async fashion
         await tcpClient.ConnectAsync(ip, 80);
         lock (_sync)
            _passed.Add(new Tuple<IPAddress, int>(ip, 80));
      }
   }
   catch (SocketException ex)
   {
      // if we failed work out why
      lock (_sync)
         _failed.Add(new Tuple<IPAddress, int,  int>(ip, 80, ex.ErrorCode));
   }
}

DataFlow

public static async Task DoWorkLoads(List<IPAddress> addresses)
{
   var options = new ExecutionDataflowBlockOptions
                     {
                        MaxDegreeOfParallelism = 50
                     };

   var block = new ActionBlock<IPAddress>(TestSocket, options);

   foreach (var ip in addresses)
      block.Post(ip);

   block.Complete();
   await block.Completion;

}

Usage

public static void Main()
{

   // create some ip addresses that might work
   var list = GenerateIPAddressList("192.168.1.1","192.168.1.5");

   // start the work load
   DoWorkLoads(list).Wait();

   // out put the results
   Console.WriteLine("passed");
   Console.WriteLine("--------");

   foreach (var result in _passed)
   {
      Console.WriteLine(result.Item1 + " : " + result.Item2);
   }

   Console.WriteLine();
   Console.WriteLine("failed");
   Console.WriteLine("--------");
   foreach (var result in _failed)
   {
      Console.WriteLine(result.Item1 + " : " + result.Item2 + " : error code = " + result.Item3);
   }
}

Output

passed
--------
127.0.0.1 : 80
1.1.1.1 : 80
87.106.238.190 : 80

failed
--------
255.70.6.101 : 80 : error code = 10051
8.8.8.8 : 80 : error code = 10060
132.159.217.202 : 80 : error code = 10060
63.14.47.133 : 80 : error code = 10060
99.5.58.81 : 80 : error code = 10060
164.105.83.243 : 80 : error code = 10060
28.162.4.38 : 80 : error code = 10060
45.255.42.252 : 80 : error code = 10060
189.71.3.147 : 80 : error code = 10060
61.77.87.169 : 80 : error code = 10060

Id usually give a working demo here, however TcpClient is forbidden on .net Fiddle

Though you can see a similar Dataflow Ping Demo here i wrote

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM