Telnet fehler beim Senden

  • VB.NET

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Lupus.

    Telnet fehler beim Senden

    hyhy

    hab hier den schönen telnet clienten:

    VB.NET-Quellcode

    1. ' -----------------------------------------------------------------------------------
    2. ' Quick easy TELNET (synchronous) quide in 100% VB.NET (no silly Asock.dll needed!!!)
    3. ' Please use this code freely NO RESTRICTIONS, change as you like.
    4. ' -----------------------------------------------------------------------------------
    5. ' This demo is intended to help developers with no experience of telnet
    6. ' to get started. The demo is just that a DEMO, it is NOT a finished product
    7. ' and MAY contain bugs.
    8. ' Rimmel©
    9. ' Please email nemo1966:gmail.com for questions/help with this code.
    10. ' -----------------------------------------------------------------------------------
    11. ' Note: the bytes received from the telnet socket will need formatting to
    12. ' get rid unwanted chars and/or converting to a different format. This demo
    13. ' simply converts to ASCII.
    14. ' -----------------------------------------------------------------------------------
    15. ' -----------------------------------------------------------------------------------
    16. ' Note: An error can occur if the recv byte lenth on the last call is exacly
    17. ' 256 bytes. Will will cause the receive call in the loop to block (waiting).
    18. ' This is where an asynchronous solution would be better.
    19. ' -----------------------------------------------------------------------------------
    20. Imports System.Net
    21. Imports System.Net.Sockets
    22. Imports System.Text
    23. Public Class Form1
    24. ' These vars can be formwide or local (in procedure) depending on how you want to use them.
    25. Dim remoteIPAddress As IPAddress
    26. Dim ep As IPEndPoint
    27. Dim tnSocket As Socket
    28. Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
    29. SendCommands("die ip", "port")
    30. End Sub
    31. Private Sub SendCommands(ByVal PIPAddress As String, ByVal PPort As String)
    32. ' LCommand: Actual command we are going to send through telnet
    33. ' Quite often this is the password or login (or both)
    34. Dim Command As String = "sys reboot" ' Example command only - you need to use your own
    35. ' LRecvString: data returned from the telnet socet
    36. Dim RecvString As String = String.Empty
    37. ' NumBytes: Number of bytes return from telnet socket (count)
    38. Dim NumBytes As Integer = 0
    39. ' Get the IP Address and the Port and create an IPEndpoint (ep)
    40. remoteIPAddress = IPAddress.Parse(PIPAddress.Trim)
    41. ep = New IPEndPoint(remoteIPAddress, CType(PPort.Trim, Integer))
    42. ' Set the socket up (type etc)
    43. tnSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    44. ' Convert the ASCII command into bytes, adding a line termination on (vbCrLf)
    45. Dim SendBytes As [Byte]() = Encoding.ASCII.GetBytes(Command & vbCrLf)
    46. ' Create a byte array for recieving bytes from the telnet socket
    47. Dim RecvBytes(255) As [Byte]
    48. Try
    49. ' Connect
    50. tnSocket.Connect(ep)
    51. Catch oEX As SocketException
    52. ' error
    53. ' You will need to do error cleanup here e.g killing the socket
    54. ' and exiting the procedure.
    55. Exit Sub
    56. End Try
    57. ' If we get to here then all seems good (we are connected)
    58. Try
    59. ' Wait a few seconds (3) (depending on connection) telnet can be slow.
    60. Wait(3000)
    61. ' Double check we are connected
    62. If tnSocket.Connected Then
    63. ' Send the command
    64. tnSocket.Send(SendBytes, SendBytes.Length, SocketFlags.None)
    65. ' -------------------------------------------------------------------
    66. ' The below "do loop" is not actually needed. This loop is used to
    67. ' Provide feedback for the commands you issue. e.g. like a hyperterm
    68. ' window. If you simply want to send command with no feedback rem it out
    69. ' -------------------------------------------------------------------
    70. ' loop getting 256 bytes of data from telnet socket at a time
    71. Do
    72. ' RecvBytes with contain 256 bytes if data returned
    73. ' numbytes with have the count of bytes returned
    74. NumBytes = tnSocket.Receive(RecvBytes, RecvBytes.Length, 0)
    75. RecvString = RecvString + Encoding.ASCII.GetString(RecvBytes, 0, NumBytes)
    76. Loop While NumBytes = 256 ' if less than 256 we are at the end of the recv stream
    77. ' Send recieved bytes to the output text box
    78. txtRecv.Text = RecvString
    79. ' -------------------------------------------------------------------
    80. Wait(1000)
    81. ' Disconnect
    82. tnSocket.Close()
    83. End If
    84. Catch oEX As Exception
    85. ' Error cleanup etc needed
    86. End Try
    87. ' Cleanup
    88. remoteIPAddress = Nothing
    89. ep = Nothing
    90. tnSocket = Nothing
    91. Command = Nothing
    92. RecvString = Nothing
    93. End Sub
    94. Private Sub Wait(ByVal PMillseconds As Integer)
    95. ' Function created to replace thread.sleep()
    96. ' Provides responsive main form without using threading.
    97. Dim TimeNow As DateTime
    98. Dim TimeEnd As DateTime
    99. Dim StopFlag As Boolean
    100. TimeEnd = Now()
    101. TimeEnd = TimeEnd.AddMilliseconds(PMillseconds)
    102. StopFlag = False
    103. While Not StopFlag
    104. TimeNow = Now()
    105. If TimeNow > TimeEnd Then
    106. StopFlag = True
    107. End If
    108. Application.DoEvents()
    109. End While
    110. ' Cleanup
    111. TimeNow = Nothing
    112. TimeEnd = Nothing
    113. End Sub
    114. Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
    115. Application.Exit()
    116. End Sub
    117. Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
    118. Dim text As [Byte]() = Encoding.ASCII.GetBytes(TextBox1.Text & vbCrLf)
    119. tnSocket.Send(text, SendBytes.Length, SocketFlags.None)
    120. End Sub
    121. 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
    Thema verschoben

    Dein Satz ergibt kein Sinn. Also das Senden von Befehlen funktioniert nicht. Welche Befehle? Wie sehen die aus? Sollen die irgendwas besonderes bewirken?

    vG,
    Lupus