我在一家时钟系统公司工作,有时无法访问路由器和其他工具来在网络上找到某些东西。
所以我做了这个,但得到了错误。我需要能够在端口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 :(得分:0)
For
循环需要数字的开头和结尾,因此您需要将txtHost1
和txtHost2
变量转换为整数。 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) : ")