I am trying to create a telnet client that will connect to several of my network devices on campus and change settings on the devices. So far I can connect with the code below, but I can't seem to get the correct return data from the device. Sometimes I get about 100 bytes and other times I get 3000 bytes. I even put a thread sleep but that still give me different return data from different devices. I know I am going to have to use an asynccallback but I just don't know how to do that. Any help would be great. I have also tried to use networkstream.dataavailable but this still is sporatic data.
<%@ Page Language="VB" debug="true"%>
<%@ Import Namespace = "System.Web" %>
<%@ Import Namespace = "System" %>
<%@ Import Namespace = "System.Net.Sockets" %>
<%@ Import NameSpace="System.Threading" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Dim tcpClient As New System.Net.Sockets.TcpClient
Dim networkStream As NetworkStream
Dim numBytesRead As Integer
Dim returndata As String
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
Dim sendBytes As [Byte]()
Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
tcpClient.Connect("10.6.8.0", 23)
networkStream = tcpClient.GetStream()
numBytesRead = networkStream.Read(bytes, 0, tcpClient.ReceiveBufferSize)
Send_Receive(vbCr, "Ctrl-Y", 0)
Send_Receive(ControlKeyPressed("Y"), "Enter Password:", 0)
Send_Receive("mypasswordis123" & vbCr, "Setup", 0)
Send_Receive("c", "#", 0)
Response.Write("<BR><BR>" & returndata)
Send_Receive("show mac-address-table vid 20 address 000b.db6e.8208" & vbCr, "#", 1000)
Response.Write("<BR><BR>" & returndata)
Send_Receive("logout" & vbCr, "Setup", 0)
sendBytes = Encoding.ASCII.GetBytes("l")
networkStream.Write(sendBytes, 0, sendBytes.Length)
'If networkStream.CanRead Then
' Response.Write("loop 1")
' Dim myReadBuffer(5000) As Byte
' Dim myCompleteMessage As StringBuilder = New StringBuilder()
' Dim numberOfBytesRead As Integer = 0
' ' Incoming message may be larger than the buffer size.
' Do
' numberOfBytesRead = networkStream.Read(bytes, 0, 1)
' myCompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(bytes, 0, 5000))
' Loop While networkStream.DataAvailable
' Thread.Sleep(100)
' ' Print out the received message to the console.
' Response.Write(("You received the following message : " + myCompleteMessage.ToString()))
'Else
' Response.Write("Sorry. You cannot read from this NetworkStream.")
'End If
tcpClient.Close()
networkStream.Close()
End Sub
' This function sends the Ctrl key to the telnet server
Function ControlKeyPressed(ByVal character)
ControlKeyPressed = Chr(Asc(character) - 64)
End Function
' This function sends data to the telnet server and waits for data to be returned from the telnet server.
Function Send_Receive(ByVal Sending As String, ByVal Finding As String, ByVal Pause As Integer)
sendBytes = Encoding.ASCII.GetBytes(Sending)
networkStream.Write(sendBytes, 0, sendBytes.Length)
' Pause let the returned data buffer complete.
Thread.Sleep(Pause)
Do
numBytesRead = networkStream.Read(bytes, 0, tcpClient.ReceiveBufferSize)
returndata = Encoding.ASCII.GetString(bytes, 0, numBytesRead)
bytes.Clear(bytes, 0, bytes.Length)
Loop While InStr(returndata, Finding) = 0
Return returndata
End Function
</script>