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 :
The output shows something like this: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
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!




Reply With Quote