Ask a questionAsk a question
 

Answerwriting a Telnet client with VB. NET

  • Monday, July 10, 2006 8:08 AMel_Fede Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi, I am trying to write a simple telnet client using VB.NET with Visual Studio 2005.

    I am using System.Net.Sockets.

    But I cant make it work yet.

    What I need is:
    To automate some tasks on UNIX server.
    To achieve this, I need to login to unix (from within my app.) via telnet, and run some commands.
    T
    hats all.

    Anyone can help me with this ?

    Thanks

    Fede.

     

     

     

     

Answers

All Replies

  • Wednesday, July 12, 2006 5:48 AMMike Flasko Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Is there a specific problem you are having with the socket communication.  If you can narrow down your question we will better be able to answer it.
  • Wednesday, July 12, 2006 11:32 AMel_Fede Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    yes,

    I'am Using

    System.Net.Sockets
    tcpClient

    By now i succeeded to connect to the unix server.

    I recieve this as a response: "??↑??▼??#??'??".

    I tried to parse it with Encoding.Ascii without success. I dont get anything.

    How to negotiate the authentication process ?

     

    Thanks

    Fede

     

     

  • Tuesday, July 18, 2006 9:11 PMTDAVISJR Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hey,

    Download this sample from

    http://www.c-sharpcorner.com/UploadFile/tylerkline/TelnetScripting11282005001158AM/TelnetScripting.aspx?ArticleID=a8e0e439-14df-4d82-82ee-8cb4c110f9a0

    Its just 1 source file that will compile to an assemply and you can use its public methods to script your telnet session. I had the same issue where I had to script a telnet session for a cisco interface using telnet. You may have to register to download the sample.

    If your not able to figure it out, let me know and I can post some sample code.

     

  • Tuesday, July 25, 2006 8:15 PMwekkaw Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi there,
     
    I've the smae issue and I've downloaded the ScriptTelnet C# project from the URL provided above.
    Now how do I implement using that class in VB.NET?
     
    I apperciate any help in advance.
  • Tuesday, July 25, 2006 9:18 PMTDAVISJR Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Ok.  The first thing you should do is make sure the downloaded project compiles ok.  Then create a new project, I would do just a Console app for testing, add a reference to the .dll file and then do something like the following:

    Imports objActiveSolutions

    Module Module1

    Sub Main()

    Dim ip As String = "66.xx.xx.xx"

    Dim port As Integer = 23

    Dim timeout As Integer = 10

    'create the object supplying the parameters

    Dim session As New ScriptingTelnet(ip, port, timeout)

    Dim connected As Boolean = session.Connect() 'try to connect

    If connected = True Then

    'connected sucessfully

    Dim startingPrompt As Integer = session.WaitFor("Username:")

    If startingPrompt = 0 Then

    'the username prompt was found so we send the username

    'and wait to receive our password prompt

    session.SendAndWait("testuser", "Password:")

    'now send the password and wait for our next prompt

    'in this sample I expect to receive a success message

    session.SendAndWait("testpassword", "Success")

    Else

    Console.WriteLine("Username prompt was not found")

    End If

    Else

    Console.WriteLine("Unable to connect to the server")

    End If

    End Sub

    End Module

     

    I hope you get the feel of how to use this component to script your session.

  • Monday, July 31, 2006 9:56 PMwekkaw Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    hey TDAVISJR thaks so much for the example above. 
     
    I compiled and used the DLL as you suggested, but I'm getting an exception.  It makes a successful connection to the server. However it waits for the "login:" prompt, and the time out occure while waiting.  I tried with longer timeout values and that didn't resolve it.  Do you know what may be causing that exception?
     
    Thanks for all the help.
    Wekkaw

  • Tuesday, August 01, 2006 2:13 AMTDAVISJR Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Well, other than making sure your looking for the correct prompt because it will time out if it don't find the prompt. Also, you could try stepping into the code and see where it fails. I'm not really an expert on the inner workings of this component. I used it and it worked. I don't think there is any contact info for the author of the code so you can shoot him an email, but you can double check that.  I don't know how much more I can help. Just do your best with it, if it doesn't work out then its back to the drawing board.
  • Tuesday, August 01, 2006 2:13 PMwekkaw Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    TDavisjr, thans so much for the help. I've tried stepping in but I'm not seeing anything yet.
    I'll keep on working on it tho. Thanks again for putting me to the right direction.
     
    Wekkaw
  • Thursday, August 10, 2006 4:27 PMVera Tiago Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I im trying to write a Telnet client for pocket Pc...can you help me?
  • Tuesday, August 15, 2006 2:09 PMEric Kowalsky Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I am having the same issue as well.  I copied and pasted the prompt for "Username: " from the telnet window that I have, but it still does not work.

    Anyone have any ideas?

    Thanks,

    Eric

  • Tuesday, August 15, 2006 2:19 PMTDAVISJR Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Guys,

    I don't know what is going on here. I was in the same situation you were all in until I found that component. I gave it a shot and it just worked. I have no clue why it worked for me and not for you all. Now, there are other components out there can can script telnet sessions; but of course they are not free. Good luck you all.

  • Tuesday, August 22, 2006 2:42 AMShave Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    This code does not work for me either.  It does not seem to be sending messages properly.

  • Thursday, October 05, 2006 2:42 PMphotonx Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I found something in http://www.codeproject.com/cs/internet/terminalcontrol.asp , look at the file TerminalControl.cs (negociating sequence commands)
  • Wednesday, January 10, 2007 9:24 AMDamsDev2007 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hello,

    This is a very good control.

    But how can I simulate "Enter" command into terminal ?

    Thanks

  • Tuesday, January 16, 2007 2:45 PMFradam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    TDAVIS or anyone that has this working in C#

    could you either post or send me some source code of how you used the code you found? I am trying to do it but it isn't working for me for some reason.

    Also did you just add the ScriptingTelnet class to your project or did you do it some other way? If some other way could I get information on how to do that as well.

    Thanks

    my email is fradam97 -at- gmail -dot- com if you would rather not post stuff.
  • Wednesday, January 17, 2007 1:34 PMFradam Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     el_Fede wrote:

    yes,

    I'am Using

    System.Net.Sockets
    tcpClient

    By now i succeeded to connect to the unix server.

    I recieve this as a response: "??↑??▼??#??'??".

    I tried to parse it with Encoding.Ascii without success. I dont get anything.

    How to negotiate the authentication process ?

    Thanks

    Fede



    To anyone having this problem and wanting to know why, I figured out that it is the Telnet server trying to negotiate with you.  If you run through the debugger (or print out) the actual byte numbers that correspond to those garbage symbols then look up negotiating a telnet connection online you will be able to figure it out.

    Here is the site I found that helped me.

    http://www.scit.wlv.ac.uk/~jphb/comms/telnet.html
  • Wednesday, February 07, 2007 6:24 AMshini Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    the code was very useful for me and it worked. i wanted to convert this console application to a windows application. That is, if the username and password in the code matches, then i should get a form with two text boxes (- one for entering the command input (like show version) and another textbox for receiving output) and a command button. where should i change the code? in dll file or the console app code?
  • Wednesday, January 30, 2008 7:08 PMSteffe81 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    add VbCrLf

     

     

    as in: Me.textbox1.text & vbcrlf

  • Friday, June 06, 2008 2:32 PMbzr7hb Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
     I have exactly same problem and any VB.NET example there?
  • Friday, June 06, 2008 5:08 PMdetach8 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Real briefly, connect a TCP socket (stream) and read the "login:" prompt, send the userid, read the "Password:" prompt, send the password, then wait for the shell "#" prompt and send the command.

    Telnet sends some basic header information which you can safely ignore. You just have to read a string off the stream and look for "login:", etc.

    Also remember that UNIX systems (if that is what you are connecting to) uses "\n" (vbLf for VB guys) not "\r\n" (vbCrLf)
    • Edited bydetach8 Friday, June 06, 2008 5:09 PMMore info
    • Edited bydetach8 Friday, June 06, 2008 5:12 PMMore details
    •  
  • Thursday, August 14, 2008 7:44 AMvalentin.georgiev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
      Hi everyone,
    I've got some problems with telneting a device too.
    I can connect to a device authenticate with username and password, but can't  execute any command after that.
    After sending some commands the receive buffer content hadn't changed. I suppose that I the commands are not send to the device.
    The device I try to connect is MSAN - Huawei UA5000.
    Can enyone help me?

    Here is the code I use:

    Sub Main()

    Dim session As New System.Net.Sockets.TcpClient

    Dim networkstream As Net.Sockets.NetworkStream

    Dim bytes(session.ReceiveBufferSize) As Byte

    Dim ne_data As String

    Dim send_command1("username".Length) As Byte

    Dim send_command2("password".Length) As Byte

    Dim send_command3("enable".Length) As Byte

    Dim send_command4("config".Length) As Byte

    Dim send_command5("test".Length) As Byte

    Dim send_command6("pots loop-line-test telno 074984046".Length + 2) As Byte

    Dim i As Integer

    session.Connect("10.240.73.132", 23)

    If session.Connected Then

    Debug.Print("connected")

    If session.GetStream.CanRead Then

    networkstream = session.GetStream

    Debug.Print("Sending user name")

    send_command1 = System.Text.Encoding.ASCII.GetBytes("username" & vbCrLf) 'with vbCrLf successfuly can sand username and password

    networkstream.Write(send_command1, 0, send_command1.Length)

    networkstream.Read(bytes, 0, CInt(session.ReceiveBufferSize))

    ne_data = System.Text.ASCIIEncoding.ASCII.GetString(bytes)

    Debug.Print("Device said:" & ne_data & vbCrLf)

    Debug.Print("Sending password")

    send_command2 = System.Text.Encoding.ASCII.GetBytes("msan" & vbCrLf)

    networkstream.Write(send_command2, 0, send_command2.Length)

    Threading.Thread.Sleep(1000) ' wait to receive data

    networkstream.Read(bytes, 0, CInt(session.ReceiveBufferSize))

    ne_data = System.Text.ASCIIEncoding.ASCII.GetString(bytes)

    Debug.Print("Device said:" & ne_data & vbCrLf)

    Debug.Print("Sending ENABLE") 'cannot send command to the device

    send_command3 = System.Text.Encoding.ASCII.GetBytes("enable" & vbLf) '??? here is the problem ???

    networkstream.Write(send_command3, 0, send_command3.Length)

    Threading.Thread.Sleep(1000) ' wait to receive data

    ne_data = System.Text.ASCIIEncoding.ASCII.GetString(bytes)

    Debug.Print("Device said:" & ne_data & vbCrLf)

    'some other code

    networkstream.Close()

    Else

    Debug.Print("can't read")

    End If

    Else

    Debug.Print("connection failed")

    End If

    session.Close()

    If session.Connected Then

    Debug.Print("still connected")

    Else

    Debug.Print("disconnected")

    End If

    End Sub

  • Friday, October 10, 2008 12:05 PMP Ryder Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am using this example and when it connect I get '??%' back so the wait for 'password:' times out.

    Any ideas?
  • Friday, October 10, 2008 3:02 PMdbasnett Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    telent is pretty complex, certainly more than just opening and writing to a port.  i wrote an application to automate a bunch of tasks using telnet.

    i used w3Sockets from Dimac.  it worked great.

    DimacWebSite
  • Wednesday, March 04, 2009 2:39 PMkabodskie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    is there any documentation for the dimac w3socket.dll?
  • Thursday, November 05, 2009 5:39 PMAlden Snow Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Broken link?