Results 1 to 7 of 7

Thread: Help on Automate telnet connection/commands

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    3

    Help on Automate telnet connection/commands

    Hi all, I am building a program that simulates exactly the telnet connection that we are using on our daily work. The process is:
    1. connect to telnet
    2. input username
    3. input password
    4. issue a command
    5. exit to telnet

    I have found a solution on how to automate command prompt but I cant make it work, below is the code :

    Code:
    Public Class Form1
        'Form code from sample project
        Private Results As String
        Private Delegate Sub delUpdate()
        Private Finished As New delUpdate(AddressOf UpdateText)
        Private Sub UpdateText()
            'txtResults.Text = Results
            txtResults.AppendText(Results)
            txtResults.SelectionStart = txtResults.Text.Length ' this line and the next
            txtResults.ScrollToCaret() 'sets the scrollbar to the bottom
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
            CMDThread.Start()
        End Sub
        Private Sub CMDAutomate()
            Dim myprocess As New Process
            Dim StartInfo As New System.Diagnostics.ProcessStartInfo
            StartInfo.FileName = "cmd" 'starts cmd window
            StartInfo.RedirectStandardInput = True
            StartInfo.RedirectStandardOutput = True
            StartInfo.UseShellExecute = False 'required to redirect
            StartInfo.CreateNoWindow = True 'creates no cmd window
            myprocess.StartInfo = StartInfo
            myprocess.Start()
            Dim SR As System.IO.StreamReader = myprocess.StandardOutput
            Dim SW As System.IO.StreamWriter = myprocess.StandardInput
            SW.WriteLine("telnet -l myuser -f c:/vb.txt myip") 'the command you wish to run.....
            SW.WriteLine("mypw") 'enter password for telnet connection
            Results = SR.ReadToEnd 'returns results of the command window
            SW.Close()
            SR.Close()
            'invokes Finished delegate, which updates textbox with the results text
            Invoke(Finished)
        End Sub
    End Class
    The output shows something like this:
    C:\Documents and Settings\Belo\my documents\visual studio 2010\Projects\WindowsApplication2\WindowsApplication2\bin\Debug>telnet -l myuser -f c:/vb.txt myip

    C:\Documents and Settings\Belo\my documents\visual studio 2010\Projects\WindowsApplication2\WindowsApplication2\bin\Debug>mypw

    The program isnt connecting. Please help me with this. thanks!

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Help on Automate telnet connection/commands

    You don't need to shell out with cmd to use telnet. Telnet is a fairly simple protocol and you can use TCPClient class to implement it.

    Strictly speaking it's a simple text terminal which sends something to the socket and receives the answer from it. Simply connect to your host using port 23.

    I have an example of POP3 client implementation here you can use the same tactics for telnet. You connect to a telnet server the same way this example connects to a pop3 server save the port you use is 23, not 110.

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    3

    Re: Help on Automate telnet connection/commands

    Hi cicatrix, thanks for the reply. Im afraid we cant use your suggestion, because the unit that i am trying to connect to is a network device and we have limited info regarding the negotiations before it accepts connection. Also I am limited to just "mimic" our daily task which we do on command prompt. Hope this can be done in VB. BTW, below is how we log in using command prompt.

    C:\Documents and Settings\Administrator>telnet myip

    then the screen clears, and...:
    login: admin
    Password:


    Starting CLI application.
    ..............................

    DNOC#

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Help on Automate telnet connection/commands

    I still can't see what prevents you from using TCPClient?

    vb Code:
    1. Dim client As Net.Sockets.TcpClient
    2. Dim sr As IO.StreamReader
    3. Dim sw As IO.StreamWriter
    4. Dim ns As Net.Sockets.NetworkStream
    5. Dim response As String
    6.  
    7. 'connecting to a server
    8. client = New Net.Sockets.TcpClient
    9. Try
    10.     client.Connect("127.0.0.1", 23) ' Change the IP to your liking.
    11. Catch e As Exception
    12.     MessageBox.Show("Failed to connect: " & e.ToString, "Error")
    13.     Return
    14. End Try
    15.  
    16.     ns = client.GetStream
    17.     sr = New IO.StreamReader(ns, Encoding.ASCII, True) ' this sends our commands to the server
    18.     sw = New IO.StreamWriter(ns)  ' and this will read server's response
    19.  
    20.     sw.WriteLine("admin")
    21.     sw.WriteLine("password")
    22.    
    23.     ' Read the stream further to get the output

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    3

    Re: Help on Automate telnet connection/commands

    Hi cicatrix, I am giving your code a try and got this errors :
    -on line 17 "encoding is not declared". this was solved by declaring "Imports System.Text".
    -on line 11, I am getting this error which I do not know how to resolve. "Variable 'e' hides a variable in an enclosing block".

    Please help and thank you so much for your patience.

  6. #6
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Help on Automate telnet connection/commands

    If you are using this inside an event, you will already have 'e' declared at the top.

    Change the e to ex

    Code:
    Catch ex As Exception
        MessageBox.Show("Failed to connect: " & ex.ToString, "Error")

  7. #7
    New Member
    Join Date
    Oct 2012
    Posts
    1

    Re: Help on Automate telnet connection/commands

    I am trying to use the above to setup a router that we use on a daily basis:>

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Button2.Text = "Please Wait..."
    Button2.Enabled = False


    Dim client As Net.Sockets.TcpClient
    Dim sr As IO.StreamReader
    Dim sw As IO.StreamWriter
    Dim ns As Net.Sockets.NetworkStream
    Dim response As String

    'connecting to a server
    client = New Net.Sockets.TcpClient
    Try
    client.Connect("127.0.0.1", 23) ' Change the IP to your liking.
    Catch ex As Exception
    MessageBox.Show("Connection Failed. Ensure Router has been factory reset and retry","Connection Error",MessageBoxButtons.OK,MessageBoxIcon.Error)
    Button2.Text = "Retry"
    Button2.Enabled = True
    Return
    End Try

    ns = client.GetStream
    sr = New IO.StreamReader(ns, Encoding.ASCII, False) ' this sends our commands to the server
    sw = New IO.StreamWriter(ns) ' and this will read server's response

    sw.WriteLine("Administrator")
    sw.WriteLine("test")
    sw.WriteLine("user flush")
    sw.WriteLine("saveall")
    sw.WriteLine("exit")

    ' Read the stream further to get the output

    Button2.Visible = False
    Button3.Visible = True


    ' Read the stream further to get the output
    End Sub

    The script runs and if the router is connected it seems to connect. However none of the commands listed above run.

    Can anyone shed any light on this. I am very new to VB so please be gentle...


    Cheers

    Rich

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width