hyhy
hab hier den schönen telnet clienten:
funktioniert alles 1a nur das senden von befehlen. leider bekomme ich auch keine fehlermeldung warum auch immer.
könnte mir jemand sagen was ich falsch gemacht habe??
thx
hab hier den schönen telnet clienten:
VB.NET-Quellcode
- ' -----------------------------------------------------------------------------------
- ' Quick easy TELNET (synchronous) quide in 100% VB.NET (no silly Asock.dll needed!!!)
- ' Please use this code freely NO RESTRICTIONS, change as you like.
- ' -----------------------------------------------------------------------------------
- ' This demo is intended to help developers with no experience of telnet
- ' to get started. The demo is just that a DEMO, it is NOT a finished product
- ' and MAY contain bugs.
- ' Rimmel©
- ' Please email nemo1966:gmail.com for questions/help with this code.
- ' -----------------------------------------------------------------------------------
- ' Note: the bytes received from the telnet socket will need formatting to
- ' get rid unwanted chars and/or converting to a different format. This demo
- ' simply converts to ASCII.
- ' -----------------------------------------------------------------------------------
- ' -----------------------------------------------------------------------------------
- ' Note: An error can occur if the recv byte lenth on the last call is exacly
- ' 256 bytes. Will will cause the receive call in the loop to block (waiting).
- ' This is where an asynchronous solution would be better.
- ' -----------------------------------------------------------------------------------
- Imports System.Net
- Imports System.Net.Sockets
- Imports System.Text
- Public Class Form1
- ' These vars can be formwide or local (in procedure) depending on how you want to use them.
- Dim remoteIPAddress As IPAddress
- Dim ep As IPEndPoint
- Dim tnSocket As Socket
- Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
- SendCommands("die ip", "port")
- End Sub
- Private Sub SendCommands(ByVal PIPAddress As String, ByVal PPort As String)
- ' LCommand: Actual command we are going to send through telnet
- ' Quite often this is the password or login (or both)
- Dim Command As String = "sys reboot" ' Example command only - you need to use your own
- ' LRecvString: data returned from the telnet socet
- Dim RecvString As String = String.Empty
- ' NumBytes: Number of bytes return from telnet socket (count)
- Dim NumBytes As Integer = 0
- ' Get the IP Address and the Port and create an IPEndpoint (ep)
- remoteIPAddress = IPAddress.Parse(PIPAddress.Trim)
- ep = New IPEndPoint(remoteIPAddress, CType(PPort.Trim, Integer))
- ' Set the socket up (type etc)
- tnSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
- ' Convert the ASCII command into bytes, adding a line termination on (vbCrLf)
- Dim SendBytes As [Byte]() = Encoding.ASCII.GetBytes(Command & vbCrLf)
- ' Create a byte array for recieving bytes from the telnet socket
- Dim RecvBytes(255) As [Byte]
- Try
- ' Connect
- tnSocket.Connect(ep)
- Catch oEX As SocketException
- ' error
- ' You will need to do error cleanup here e.g killing the socket
- ' and exiting the procedure.
- Exit Sub
- End Try
- ' If we get to here then all seems good (we are connected)
- Try
- ' Wait a few seconds (3) (depending on connection) telnet can be slow.
- Wait(3000)
- ' Double check we are connected
- If tnSocket.Connected Then
- ' Send the command
- tnSocket.Send(SendBytes, SendBytes.Length, SocketFlags.None)
- ' -------------------------------------------------------------------
- ' The below "do loop" is not actually needed. This loop is used to
- ' Provide feedback for the commands you issue. e.g. like a hyperterm
- ' window. If you simply want to send command with no feedback rem it out
- ' -------------------------------------------------------------------
- ' loop getting 256 bytes of data from telnet socket at a time
- Do
- ' RecvBytes with contain 256 bytes if data returned
- ' numbytes with have the count of bytes returned
- NumBytes = tnSocket.Receive(RecvBytes, RecvBytes.Length, 0)
- RecvString = RecvString + Encoding.ASCII.GetString(RecvBytes, 0, NumBytes)
- Loop While NumBytes = 256 ' if less than 256 we are at the end of the recv stream
- ' Send recieved bytes to the output text box
- txtRecv.Text = RecvString
- ' -------------------------------------------------------------------
- Wait(1000)
- ' Disconnect
- tnSocket.Close()
- End If
- Catch oEX As Exception
- ' Error cleanup etc needed
- End Try
- ' Cleanup
- remoteIPAddress = Nothing
- ep = Nothing
- tnSocket = Nothing
- Command = Nothing
- RecvString = Nothing
- End Sub
- Private Sub Wait(ByVal PMillseconds As Integer)
- ' Function created to replace thread.sleep()
- ' Provides responsive main form without using threading.
- Dim TimeNow As DateTime
- Dim TimeEnd As DateTime
- Dim StopFlag As Boolean
- TimeEnd = Now()
- TimeEnd = TimeEnd.AddMilliseconds(PMillseconds)
- StopFlag = False
- While Not StopFlag
- TimeNow = Now()
- If TimeNow > TimeEnd Then
- StopFlag = True
- End If
- Application.DoEvents()
- End While
- ' Cleanup
- TimeNow = Nothing
- TimeEnd = Nothing
- End Sub
- Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
- Application.Exit()
- End Sub
- Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
- Dim text As [Byte]() = Encoding.ASCII.GetBytes(TextBox1.Text & vbCrLf)
- tnSocket.Send(text, SendBytes.Length, SocketFlags.None)
- End Sub
- End Class
funktioniert alles 1a nur das senden von befehlen. leider bekomme ich auch keine fehlermeldung warum auch immer.
könnte mir jemand sagen was ich falsch gemacht habe??
thx