2015-01-07 150 views
2

我有一個表單,我可以讓我選擇一條線並重置線上的所有端口。這是通過Telnet完成的。我知道如何Socket和發送IP地址我希望工作,但我不明白的是發送命令登錄和重置端口。發送命令到Telnet

設置是,當爲不同的行選擇多個複選框中的一個時,它會在私人子上調用一行,然後開始下一行。

我一直在網上搜索幾天沒有。我試過的最後一個代碼如下:

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 

我是新來使用Telnet與vb.net的頂部以下。但是,爲什麼在vb.net和Cmd.exe中這麼做很困難,它只需要六個命令?

+1

你應該看看你正在使用的協議的文檔。通過快速查看代碼,您沒有指定要發送的數據的長度,通常這是通過標題或以某種方式終止字符串(空/空格)來完成的。此外,你認爲在閱讀時你會得到所有想要的信息。這不是真的,讀取一次只能返回1個字節。 –

+0

我會老實說我已經複製並嘗試重複使用這段代碼。我完全失去了。試圖研究每條線的作用......沒有太多。 – Alenhj

回答

2

歐凱隊友,這裏是你正在尋找的代碼,但重點:

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