1

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

Community
  • 1
  • 1

1 Answers1

0

Probably, you need to ONLY send Line Feed (vbLf). sending CR+LF may generate unexpected behavior. Try sending only LF (Line Feed). That may help. See this Wikipedia article: http://en.wikipedia.org/wiki/Newline#Representations

Also, here is the SSH way to do this: executing commands on unix server via visual basic application.

But, if you prefer to stick with Telnet, which it considered less secure then SSH, read this: http://social.msdn.microsoft.com/Forums/en-US/bf432ec4-cfdf-44be-a34a-5fdaf8115eb1/writing-a-telnet-client-with-vb-net?forum=netfxnetcom. Here is a direct link to the library that was referred to in the previously mentioned link: http://www.c-sharpcorner.com/UploadFile/tylerkline/TelnetScripting11282005001158AM/TelnetScripting.aspx?ArticleID=a8e0e439-14df-4d82-82ee-8cb4c110f9a0. I personally like to use something else already made, so I would not need to "re-invent the wheel", like they say.

Community
  • 1
  • 1
Maxwell175
  • 1,954
  • 1
  • 17
  • 27