向 Telnet 发送命令
Sending Commands to Telnet
我正在制作一个表格,它让我 select 一条线并重置线上的所有端口。这是通过 Telnet 完成的。我了解如何套接字并发送我希望使用的 IP 地址,但我不明白的是发送命令以登录和重置端口。
设置是,当多个复选框之一被 selected 用于不同的行时,它会调用私有子程序来运行一行,然后再开始下一行。
我这几天一直在网上搜索没有。我最后尝试的代码如下:
Dim TelnetClient As TcpClient
Dim ThisStream As NetworkStream
Dim SendBuffer(128) As Byte
Dim ReadBuffer(128) As Byte
Dim ReturnVal As String
Dim ReturnLength As Integer
TelnetClient = New TcpClient("Ip Address", 23)
ThisStream = TelnetClient.GetStream
SendBuffer = System.Text.Encoding.ASCII.GetBytes("Username")
ThisStream.Write(SendBuffer, 0, SendBuffer.Length)
ReturnLength = ThisStream.Read(ReadBuffer, 0, ReadBuffer.Length)
ReturnVal = System.Text.Encoding.ASCII.GetString(ReadBuffer)
SendBuffer = System.Text.Encoding.ASCII.GetBytes("Password")
ThisStream.Write(SendBuffer, 0, SendBuffer.Length)
我一直在兜圈子试图理解这一点。
我曾尝试通过 cmd.exe 进行 Telnet,但我总是返回错误并放弃了该路由。
我也看到过用代码在Telnet中找词
示例:
If message.ToString.EndsWith("login:") Then
Await WriteStringAsync("username", stream
但不能 100% 确定如何完全适应我可以使用的内容。
任何帮助表示赞赏。
谢谢。
编辑:添加信息。
我在代码列表的顶部有以下内容
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
我是 vb.net 使用 Telnet 的新手。但是,为什么在 vb.net 中很难做到这一点,而在 Cmd.exe 中只需要六个命令?
好的,伙计,这是您要查找的代码,但重点是:
Dim Full_Stop As String = ""
Dim TelnetClient As New TcpClient
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
TelnetClient.Connect("IP ADDRESS", 23) 'Connecting to the IP Given
Send_Sub("Start Connection Command")
Dim thr As New Threading.Thread(AddressOf Receive_thread)
thr.Start()
End Sub
Private Sub StopButton_Click(sender As Object, e As EventArgs) Handels StopButton.Click
Full_Stop = "Stop"
TelnetClient.Close()
End Sub
Sub Send_Sub(ByVal msg As String)
Dim byt_to_send() As Byte = System.Text.Encoding.ASCII.GetBytes(msg)
TelnetClient.Client.Send(byt_to_send, 0, byt_to_send.Length, SocketFlags.None)
End Sub
Sub Receive_thread()
re:
If Full_Stop = "Stop" Then Exit Sub 'If you set Full_Stop string to "Stop" the thread will end
If TelnetClient.Client.Available < 0 Then 'Check if there is any Data to receive
Dim byt_to_receive(TelnetClient.Available - 1) As Byte
TelnetClient.Client.Receive(byt_to_receive, 0, byt_to_receive.Length, SocketFlags.None)
Dim String_From_Byte As String = System.Text.Encoding.ASCII.GetString(byt_to_receive)
If String_From_Byte = "login:" Then 'If the telnet asks you to Enter the login name the Send_Sub will do the job
Send_Sub("username")
ElseIf String_From_Byte = "password:" Then 'If the telnet asks you to Enter the Password the Send_Sub will do the job
Send_Sub("password")
End If
End If
GoTo re 'this will NOT allow the thread to End by sending it back to re: statement, unless the Full_Stop is "Stop"
End Sub
我正在制作一个表格,它让我 select 一条线并重置线上的所有端口。这是通过 Telnet 完成的。我了解如何套接字并发送我希望使用的 IP 地址,但我不明白的是发送命令以登录和重置端口。
设置是,当多个复选框之一被 selected 用于不同的行时,它会调用私有子程序来运行一行,然后再开始下一行。
我这几天一直在网上搜索没有。我最后尝试的代码如下:
Dim TelnetClient As TcpClient
Dim ThisStream As NetworkStream
Dim SendBuffer(128) As Byte
Dim ReadBuffer(128) As Byte
Dim ReturnVal As String
Dim ReturnLength As Integer
TelnetClient = New TcpClient("Ip Address", 23)
ThisStream = TelnetClient.GetStream
SendBuffer = System.Text.Encoding.ASCII.GetBytes("Username")
ThisStream.Write(SendBuffer, 0, SendBuffer.Length)
ReturnLength = ThisStream.Read(ReadBuffer, 0, ReadBuffer.Length)
ReturnVal = System.Text.Encoding.ASCII.GetString(ReadBuffer)
SendBuffer = System.Text.Encoding.ASCII.GetBytes("Password")
ThisStream.Write(SendBuffer, 0, SendBuffer.Length)
我一直在兜圈子试图理解这一点。
我曾尝试通过 cmd.exe 进行 Telnet,但我总是返回错误并放弃了该路由。
我也看到过用代码在Telnet中找词
示例:
If message.ToString.EndsWith("login:") Then
Await WriteStringAsync("username", stream
但不能 100% 确定如何完全适应我可以使用的内容。 任何帮助表示赞赏。
谢谢。
编辑:添加信息。
我在代码列表的顶部有以下内容
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
我是 vb.net 使用 Telnet 的新手。但是,为什么在 vb.net 中很难做到这一点,而在 Cmd.exe 中只需要六个命令?
好的,伙计,这是您要查找的代码,但重点是:
Dim Full_Stop As String = ""
Dim TelnetClient As New TcpClient
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
TelnetClient.Connect("IP ADDRESS", 23) 'Connecting to the IP Given
Send_Sub("Start Connection Command")
Dim thr As New Threading.Thread(AddressOf Receive_thread)
thr.Start()
End Sub
Private Sub StopButton_Click(sender As Object, e As EventArgs) Handels StopButton.Click
Full_Stop = "Stop"
TelnetClient.Close()
End Sub
Sub Send_Sub(ByVal msg As String)
Dim byt_to_send() As Byte = System.Text.Encoding.ASCII.GetBytes(msg)
TelnetClient.Client.Send(byt_to_send, 0, byt_to_send.Length, SocketFlags.None)
End Sub
Sub Receive_thread()
re:
If Full_Stop = "Stop" Then Exit Sub 'If you set Full_Stop string to "Stop" the thread will end
If TelnetClient.Client.Available < 0 Then 'Check if there is any Data to receive
Dim byt_to_receive(TelnetClient.Available - 1) As Byte
TelnetClient.Client.Receive(byt_to_receive, 0, byt_to_receive.Length, SocketFlags.None)
Dim String_From_Byte As String = System.Text.Encoding.ASCII.GetString(byt_to_receive)
If String_From_Byte = "login:" Then 'If the telnet asks you to Enter the login name the Send_Sub will do the job
Send_Sub("username")
ElseIf String_From_Byte = "password:" Then 'If the telnet asks you to Enter the Password the Send_Sub will do the job
Send_Sub("password")
End If
End If
GoTo re 'this will NOT allow the thread to End by sending it back to re: statement, unless the Full_Stop is "Stop"
End Sub