如何扫描特定端口上的一系列IP地址

时间:2018-04-10 06:27:26

标签: c# tcp

我试图弄清楚如何扫描一系列IP地址中的特定TCP端口?

到目前为止,这是我的代码:

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...
}

1 个答案:

答案 0 :(得分:1)

您可以使用TcpClient类来检查您的端口,并使用ActionBlock Classasync / await模式(适用于IO绑定操作)并行运行在TPL dataflow库中。

您可以像这样生成您的ips

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

要在async 并行 IO 中使用它,请先创建您的工作方法

主要方法

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));
   }
}

<强>数据流

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;

}

<强>用法

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);
   }
}

输出

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

我通常会在这里提供一个有效的演示,但是{。1}}是禁止的.net小提琴

虽然你可以看到类似的Dataflow Ping Demo here我写的