|
-
Sep 20th, 2010, 11:11 PM
#1
Thread Starter
New Member
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!
-
Sep 21st, 2010, 01:04 AM
#2
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.
-
Sep 21st, 2010, 05:45 PM
#3
Thread Starter
New Member
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#
-
Sep 22nd, 2010, 01:29 AM
#4
Re: Help on Automate telnet connection/commands
I still can't see what prevents you from using TCPClient?
vb Code:
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 e As Exception
MessageBox.Show("Failed to connect: " & e.ToString, "Error")
Return
End Try
ns = client.GetStream
sr = New IO.StreamReader(ns, Encoding.ASCII, True) ' this sends our commands to the server
sw = New IO.StreamWriter(ns) ' and this will read server's response
sw.WriteLine("admin")
sw.WriteLine("password")
' Read the stream further to get the output
-
Sep 22nd, 2010, 10:25 AM
#5
Thread Starter
New Member
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.
-
Sep 22nd, 2010, 11:11 AM
#6
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")
-
Oct 30th, 2012, 11:02 AM
#7
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|