2016-08-05 83 views
0

我爲時鐘系統公司工作,有時無法訪問路由器和其他工具來查找網絡中的某些東西。掃描在端口9922上運行的東西的內部IP範圍VBNet

所以我做了這個,但得到的錯誤。我需要能夠掃描端口9922,TCP上的內部網絡。

Imports System.Net.Sockets 
Imports System.Net 
Imports System.Threading 
Imports System.Text 
Module Module1 
    Dim portStart As Integer 
    Dim portEnd As Integer 
    Dim lngPort As Long 
    Dim lngRange As Long 
    Dim txtHost1 As String 
    Dim txtHost2 As String 
    Dim txtHost As String 
    Dim openPorts As Integer 
    Dim closedPorts As Integer 
    Dim range As String 
    Dim lgnRange2 As Long 
    Sub Main() 
     Console.Write("Range Start: (For example 192.168.1.1 = 192.168.1. ") 
     range = Console.ReadLine() 

     Console.Write("Host to: Example 1 ") 
     txtHost1 = Console.ReadLine() 

     Console.Write("Host from: Example 254 ") 
     txtHost2 = Console.ReadLine() 

     Console.Write("Port: (9922 for Face Rec By default)") 
     portEnd = Console.ReadLine() 
     For lngRange = txtHost1 To txtHost2 

     Next 
     For lgnRange2 = range + lngRange 
      Dim myTcpClient As New TcpClient() 
      Try 
       myTcpClient.Connect(lngPort, portEnd) 
       Console.WriteLine("Host: " + txtHost + " : ") 
       Console.WriteLine(" Port " + lngPort.ToString() + " : Open :") 
       openPorts += 1 
       myTcpClient.Close() 
      Catch ex As SocketException 
       Console.WriteLine("Host: " + txtHost + " : ") 
       Console.WriteLine(" Port " + lngPort.ToString() + " : Closed :") 
       ' Console.WriteLine(ex.Message) 
       closedPorts += 1 
       Console.Write(" " & openPorts.ToString & " open port(s) : ") 
       Console.Write("  " & closedPorts.ToString & " closed port(s) : ") 

       Console.Beep() 
       Console.Write(txtHost + " : " + portStart.ToString + " - " + portEnd.ToString + " : Scanned Sucessfully") 

      End Try 
     Next 
    End Sub 
    Public Class psAPP 
     Public Shared Sub Main() 

     End Sub 
    End Class 
End Module 
+0

你應該提供比「所以我做了這個,但得到的錯誤已經」更多的細節 - 什麼錯誤是什麼線?運行時或編譯錯誤?等等。回顧[問]獲取更多提示。 – Mark

+0

這行看起來不正確。對於lgnRange2 = range + lngRange'。 – Mark

+0

對不起,遲到的迴應, –

回答

0

一個For循環需要一個數字開始和結束,所以你需要將txtHost1txtHost2變量轉換爲整數。 TcpClient.Connect調用將需要一個IPAddress或從您的硬編碼前綴和當前主機號碼構建的主機名。所以,你的循環可能看起來像(未經測試):

Option Strict On 
Option Infer On 
'... 
For i = Integer.Parse(txtHost1) To Integer.Parse(txtHost2) 
    Dim remoteAddr = IPAddress.Parse("192.168.1." & i) 
    Try 
     Using myTcpClient = New TcpClient() 
      myTcpClient.Connect(remoteAddr, Integer.Parse(portEnd)) 
     End Using 
     Console.WriteLine("Host: " + txtHost + " : ") 
     Console.WriteLine(" Port " + lngPort.ToString() + " : Open :") 
     openPorts += 1 
    Catch ex As SocketException 
     Console.WriteLine("Host: " + txtHost + " : ") 
     Console.WriteLine(" Port " + lngPort.ToString() + " : Closed :") 
     closedPorts += 1 
    End Try 
Next 
Console.Write(" " & openPorts.ToString & " open port(s) : ") 
Console.Write("  " & closedPorts.ToString & " closed port(s) : ")