tABS

Saturday, May 14, 2011

Ping IpAddress with Port number?

If your looking for a port scanner, use somthing like...


Code Snippet
Public Sub PortScan()
        Console.Clear()
        Console.WriteLine("Please Enter IP address: ")
        Dim IP As String = Console.ReadLine()
        Console.WriteLine("Please Enter Starting Port: ")
        Dim StartPort As Integer = Console.ReadLine()
        Console.WriteLine("Please Enter End Port: ")
        Dim EndPort As Integer = Console.ReadLine()
        Dim ListOfPorts As New List(Of Integer)
        Try
            For Port As Integer = StartPort To EndPort
                Console.SetCursorPosition(0, 7)
                Console.Write("Current Port: " & Port)
                If ScanPort(System.Net.IPAddress.Parse(IP), Port) = True Then
                    ListOfPorts.Add(Port)
                End If
            Next
       
            Console.WriteLine(vbCrLf & "List of Open Ports on: " & IP & " (" & System.Net.Dns.GetHostEntry(System.Net.IPAddress.Parse(IP)).HostName & ")")
            For Each Port As Integer In ListOfPorts
                Console.Write(Port & ", ")
            Next
        Catch
            Console.WriteLine(vbCrLf & "Invalid IP specified, Please try again...")
        End Try
    End Sub

    ReadOnly Property ScanPort(ByVal IP As System.Net.IPAddress, ByVal Port As Integer) As Boolean
        Get
            Dim TCP As New System.Net.Sockets.TcpClient
            Try
                TCP.Connect(IP, Port)
            Catch
            End Try
            If TCP.Connected = True Then
                ScanPort = True
                TCP.Close()
            Else
                ScanPort = False
                TCP.Close()
            End If
        End Get
    End Property

It's a bit slow, but if you run the 'ScanPort' property on several threads then you can speed the process up considerably.

No comments:

Post a Comment