向 Telnet 发送命令 [英] Sending Commands to Telnet

查看:58
本文介绍了向 Telnet 发送命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在制作的表格,可以让我选择一条线路并重置线路上的所有端口.这是通过 Telnet 完成的.我了解如何通过套接字连接并发送我希望使用的 IP 地址,但我不明白的是发送登录和重置端口的命令.

I have a Form that I am making that lets me select a line and reset all of the port on the line. This is done through Telnet. I understand how to Socket and send the IP Address I wish to work with but what I do not understand is sending the commands to log in and reset the ports.

设置是,当为不同的线路选择多个复选框时,它会调用私有子来运行一行,然后再开始下一个.

The set up is that when one of more check boxes are selected for the different line it calls on private sub to run a line before starting on the next one.

我已经在网上搜索了几天没有.我尝试的最后一个代码如下:

I have been web searching for a few days not. The last code I tried was the following:

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)

我一直在绕圈子试图理解这一点.

I have been going around in circle trying to understand this.

我曾尝试通过 cmd.exe 执行 Telnet,但我不断返回错误并放弃了该路线.

I have tried doing the Telnet through cmd.exe but I keep coming back with errors and abandoned that route.

我还看到使用代码在 Telnet 中查找单词.

I have also seen using code to find words in the Telnet.

示例:

If message.ToString.EndsWith("login:") Then
   Await WriteStringAsync("username", stream

但不能 100% 确定如何完全适应我可以使用的内容.任何帮助表示赞赏.

But not 100% sure on how to fully adapt it to what I can use. Any help is appreciated.

谢谢.

添加信息.

我在代码列表的顶部有以下内容

I have the following at the top of the code list

Imports System.IO
Imports System.Net
Imports System.Net.Sockets

我是在 vb.net 中使用 Telnet 的新手.但是,为什么在 vb.net 中很难做到这一点,而在 Cmd.exe 中只需要六个命令?

I am new to using Telnet with vb.net. However, why is it so hard to do this in vb.net and in Cmd.exe it only takes six commands?

推荐答案

好吧,伙计,这里是您正在寻找的代码,但请注意:

Okey mate , here is the code you are looking for , but focus :

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

这篇关于向 Telnet 发送命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆