I am trying to automate a number of unix commands that need to be executed on a remote unix server through VB.NET Currently I would open command prompt, connect to the host with telnet , issue my commands and exit the session. As part of a larger project I would like to automate this. I have been researching different methods to setup network connections with VB for a few days now and I'm not getting closer to a solution. I have tried using a 3rd party dll library (as suggersted here: executing commands on unix server via visual basic application) as reference to my project but I get a timeout error that I can't resolve after trying to simply get a directory listing as a test.
I tried coding it myself with a TcpClient but my code just hangs after the first response (see code & output below). I have no experience with networking, ports or sockets other than what I have read over the last few days. I have not security concerns with the connections, I'm on my company's intranet. Any help to resolve the issue would be greatly appreciated.
Code: Primarily cut and paste from MSDN VB example for TcpClient
Dim message As String
'Connect to Server
Dim port As Int32 = 23
Dim client As New TcpClient(unixServer, port)
'Send username to login to server
message = userName & " \n"
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
Dim stream As NetworkStream = client.GetStream
stream.Write(data, 0, data.Length)
TextBox2.AppendText("Sent: {0} " & message & vbCrLf)
data = New [Byte](256) {}
Dim responseData As [String] = String.Empty
Dim bytes As Int32 = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
TextBox2.AppendText("Recieved: {0} " & responseData & vbCrLf)
'Send password to login to server
message = passWord & " \n"
data = New [Byte](256) {}
data = System.Text.Encoding.ASCII.GetBytes(message)
stream.Write(data, 0, data.Length)
TextBox2.AppendText("Sent: {0} " & message & vbCrLf)
data = New [Byte](256) {}
bytes = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
TextBox2.AppendText("Recieved: {0} " & responseData & vbCrLf)
'Send return key to start new line
message = "\n"
data = New [Byte](256) {}
data = System.Text.Encoding.ASCII.GetBytes(message)
stream.Write(data, 0, data.Length)
TextBox2.AppendText("Sent: {0} " & message & vbCrLf)
data = New [Byte](256) {}
bytes = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
TextBox2.AppendText("Recieved: {0} " & responseData & vbCrLf)
'Get directory listing
message = "ls -lrt"
data = New [Byte](256) {}
data = System.Text.Encoding.ASCII.GetBytes(message)
stream.Write(data, 0, data.Length)
TextBox2.AppendText("Sent: {0} " & message & vbCrLf)
data = New [Byte](256) {}
bytes = stream.Read(data, 0, data.Length)
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
TextBox2.AppendText("Recieved: {0} " & responseData & vbCrLf)
stream.Close()
client.Close()
Output to textbox:
Sent: {0} username \n
Recieved: {0} ???? ??#??$
Sent: {0} password \n