473,408 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Using PLink interactively as a Telnet process with VB.Net 2003

Al
I'm currently attempting to use PLink (the console component of PUTTY - see
http://www.chiark.greenend.org.uk/~sgtatham/putty/) as a Telnet component as
I may in future need to change to using SSH and this seems an ideal solution.
I'm running it as a process and re-directing the standardinput/output/error

However, despite working through all the different variations of code I can
either think of or find I am unable to achieve true interactivity with the
program (which would be nice). I can happily pass commands to the program,
but once it has connected to the Telnet destination (i.e. Once I have passed
the password), I do not get any output returned to standardoutput until PLink
exits, at which point I get it all. I have tried reading the
standardoutput/error on different threads to see if they were blocking the
processing, but this did not seem to be the case, so I dropped back to the
simpler more hackable code you see below.

I have attached the main block below, it's not very neat due to being
reworked multiple times and may now contain some redundant bits so apologies
in advance. Hopefully however it may provide a basis for someone to point out
what I need to change to get this thing working.

Friend Shared TelnetProcess As System.diagnostics.Process = New
System.diagnostics.Process
Friend Shared TelnetStage As Integer = 0
Friend Shared TelnetBuffer As String = ""
Friend Shared TelnetBufferFull As String = ""
Friend Shared TelnetIn As StreamWriter
Friend Shared TelnetOut As StreamReader
Friend Shared TelnetErr As StreamReader
Friend WithEvents tmrProcessAIX As System.Timers.Timer = New
System.Timers.Timer
Friend WithEvents tmrProcessSwitch As System.Timers.Timer = New
System.Timers.Timer
Friend WithEvents objStdOutRead As clsStdOutRead = New clsStdOutRead
Friend WithEvents objStdErrRead As clsStdErrRead = New clsStdErrRead
Friend thrdProcessStdOutRead As Thread = New Thread(AddressOf
objStdOutRead.StdOutRead)
Friend thrdProcessStdErrRead As Thread = New Thread(AddressOf
objStdErrRead.StdErrRead)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

TextBox1.Text = ""

TelnetStage = 0
TelnetBuffer = ""
TelnetBufferFull = ""

TelnetProcess.StartInfo.FileName = Application.StartupPath &
"\tools\" & "plink.exe"
TelnetProcess.StartInfo.Arguments = " -telnet 192.168.1.6 -l myid"
TelnetProcess.StartInfo.UseShellExecute = False
TelnetProcess.StartInfo.CreateNoWindow = True
TelnetProcess.StartInfo.RedirectStandardInput = True
TelnetProcess.StartInfo.RedirectStandardOutput = True
'TelnetProcess.StartInfo.RedirectStandardError = True
TelnetProcess.Start()
TelnetIn = TelnetProcess.StandardInput
TelnetOut = TelnetProcess.StandardOutput
TelnetErr = TelnetProcess.StandardOutput
TelnetProcess.StandardInput.AutoFlush = True
tmrProcess.Interval = 1000
tmrProcess.Enabled = True

End Sub

Sub tmrProcess_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles tmrProcessAIX.Elapsed

Dim sOutBuffer As String = ""
Dim x As String
Dim iChar As Integer

Try
Select Case TelnetStage
Case 0
tmrProcessAIX.Enabled = False
Do While TelnetProcess.StandardOutput.Peek() >= 0
iChar = TelnetProcess.StandardOutput.Read()
TelnetBuffer = TelnetBuffer & (Convert.ToChar(iChar))
Console.WriteLine("TB >" & TelnetBuffer.ToString())
Loop
If (InStr(TelnetBuffer, "Password:") > 0) Then
TelnetProcess.StandardInput.Write("password" & vbCrLf)
TelnetBufferFull = TelnetBufferFull & TelnetBuffer &
"xxxxxxxx" & System.Environment.NewLine
TelnetBuffer = ""
TelnetStage = 1
End If
tmrProcessAIX.Enabled = True
Case 1
tmrProcessAIX.Enabled = False
Do While TelnetProcess.StandardOutput.Peek() >= 0
iChar = TelnetProcess.StandardOutput.Read()
TelnetBuffer = TelnetBuffer & (Convert.ToChar(iChar))
Console.WriteLine("TB >" & TelnetBuffer.ToString())
Loop
TelnetProcess.StandardInput.WriteLine("command" & vbCrLf)
TelnetBufferFull = TelnetBufferFull & TelnetBuffer
TelnetBuffer = ""
TelnetStage = 98
tmrProcessAIX.Enabled = True

Case 98
tmrProcessAIX.Enabled = False
Do While TelnetProcess.StandardOutput.Peek() >= 0
iChar = TelnetProcess.StandardOutput.Read()
TelnetBuffer = TelnetBuffer & (Convert.ToChar(iChar))
Console.WriteLine("TB >" & TelnetBuffer.ToString())
Loop
TelnetProcess.StandardInput.WriteLine("exit" & vbCrLf)
TelnetProcess.StandardInput.WriteLine("exit" & vbCrLf)
TelnetBufferFull = TelnetBufferFull & TelnetBuffer
TelnetBuffer = ""
TelnetStage = 99
Console.WriteLine("TBF>" & TelnetBufferFull)
tmrProcessAIX.Enabled = True
Case 99
tmrProcessAIX.Enabled = False
Do While TelnetProcess.StandardOutput.Peek() >= 0
iChar = TelnetProcess.StandardOutput.Read()
TelnetBuffer = TelnetBuffer & (Convert.ToChar(iChar))
Console.WriteLine("TB >" & TelnetBuffer.ToString())
Loop
TelnetBufferFull = TelnetBufferFull & TelnetBuffer
x = TelnetBufferFull &
TelnetProcess.StandardOutput.ReadToEnd
'MsgBox(x)
TextBox1.Text = x
TelnetProcess.Kill()
End Select

Catch ioe As InvalidOperationException
Catch ex As Exception
MsgBox(ex.ToString)
Finally
End Try

End Sub
--
Al
Systems Type
Apr 3 '06 #1
2 8614
Consider using Telnet Factory for .NET and SSH Factory for .NET
together to obtain both telnet and ssh scripting functionality:

http://www.jscape.com/telnetfactorydotnet/

http://www.jscape.com/sshfactorydotnet/
Al wrote:
I'm currently attempting to use PLink (the console component of PUTTY - see
http://www.chiark.greenend.org.uk/~sgtatham/putty/) as a Telnet component as
I may in future need to change to using SSH and this seems an ideal solution.
I'm running it as a process and re-directing the standardinput/output/error

However, despite working through all the different variations of code I can
either think of or find I am unable to achieve true interactivity with the
program (which would be nice). I can happily pass commands to the program,
but once it has connected to the Telnet destination (i.e. Once I have passed
the password), I do not get any output returned to standardoutput until PLink
exits, at which point I get it all. I have tried reading the
standardoutput/error on different threads to see if they were blocking the
processing, but this did not seem to be the case, so I dropped back to the
simpler more hackable code you see below.

I have attached the main block below, it's not very neat due to being
reworked multiple times and may now contain some redundant bits so apologies
in advance. Hopefully however it may provide a basis for someone to point out
what I need to change to get this thing working.

Friend Shared TelnetProcess As System.diagnostics.Process = New
System.diagnostics.Process
Friend Shared TelnetStage As Integer = 0
Friend Shared TelnetBuffer As String = ""
Friend Shared TelnetBufferFull As String = ""
Friend Shared TelnetIn As StreamWriter
Friend Shared TelnetOut As StreamReader
Friend Shared TelnetErr As StreamReader
Friend WithEvents tmrProcessAIX As System.Timers.Timer = New
System.Timers.Timer
Friend WithEvents tmrProcessSwitch As System.Timers.Timer = New
System.Timers.Timer
Friend WithEvents objStdOutRead As clsStdOutRead = New clsStdOutRead
Friend WithEvents objStdErrRead As clsStdErrRead = New clsStdErrRead
Friend thrdProcessStdOutRead As Thread = New Thread(AddressOf
objStdOutRead.StdOutRead)
Friend thrdProcessStdErrRead As Thread = New Thread(AddressOf
objStdErrRead.StdErrRead)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

TextBox1.Text = ""

TelnetStage = 0
TelnetBuffer = ""
TelnetBufferFull = ""

TelnetProcess.StartInfo.FileName = Application.StartupPath &
"\tools\" & "plink.exe"
TelnetProcess.StartInfo.Arguments = " -telnet 192.168.1.6 -l myid"
TelnetProcess.StartInfo.UseShellExecute = False
TelnetProcess.StartInfo.CreateNoWindow = True
TelnetProcess.StartInfo.RedirectStandardInput = True
TelnetProcess.StartInfo.RedirectStandardOutput = True
'TelnetProcess.StartInfo.RedirectStandardError = True
TelnetProcess.Start()
TelnetIn = TelnetProcess.StandardInput
TelnetOut = TelnetProcess.StandardOutput
TelnetErr = TelnetProcess.StandardOutput
TelnetProcess.StandardInput.AutoFlush = True
tmrProcess.Interval = 1000
tmrProcess.Enabled = True

End Sub

Sub tmrProcess_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles tmrProcessAIX.Elapsed

Dim sOutBuffer As String = ""
Dim x As String
Dim iChar As Integer

Try
Select Case TelnetStage
Case 0
tmrProcessAIX.Enabled = False
Do While TelnetProcess.StandardOutput.Peek() >= 0
iChar = TelnetProcess.StandardOutput.Read()
TelnetBuffer = TelnetBuffer & (Convert.ToChar(iChar))
Console.WriteLine("TB >" & TelnetBuffer.ToString())
Loop
If (InStr(TelnetBuffer, "Password:") > 0) Then
TelnetProcess.StandardInput.Write("password" & vbCrLf)
TelnetBufferFull = TelnetBufferFull & TelnetBuffer &
"xxxxxxxx" & System.Environment.NewLine
TelnetBuffer = ""
TelnetStage = 1
End If
tmrProcessAIX.Enabled = True
Case 1
tmrProcessAIX.Enabled = False
Do While TelnetProcess.StandardOutput.Peek() >= 0
iChar = TelnetProcess.StandardOutput.Read()
TelnetBuffer = TelnetBuffer & (Convert.ToChar(iChar))
Console.WriteLine("TB >" & TelnetBuffer.ToString())
Loop
TelnetProcess.StandardInput.WriteLine("command" & vbCrLf)
TelnetBufferFull = TelnetBufferFull & TelnetBuffer
TelnetBuffer = ""
TelnetStage = 98
tmrProcessAIX.Enabled = True

Case 98
tmrProcessAIX.Enabled = False
Do While TelnetProcess.StandardOutput.Peek() >= 0
iChar = TelnetProcess.StandardOutput.Read()
TelnetBuffer = TelnetBuffer & (Convert.ToChar(iChar))
Console.WriteLine("TB >" & TelnetBuffer.ToString())
Loop
TelnetProcess.StandardInput.WriteLine("exit" & vbCrLf)
TelnetProcess.StandardInput.WriteLine("exit" & vbCrLf)
TelnetBufferFull = TelnetBufferFull & TelnetBuffer
TelnetBuffer = ""
TelnetStage = 99
Console.WriteLine("TBF>" & TelnetBufferFull)
tmrProcessAIX.Enabled = True
Case 99
tmrProcessAIX.Enabled = False
Do While TelnetProcess.StandardOutput.Peek() >= 0
iChar = TelnetProcess.StandardOutput.Read()
TelnetBuffer = TelnetBuffer & (Convert.ToChar(iChar))
Console.WriteLine("TB >" & TelnetBuffer.ToString())
Loop
TelnetBufferFull = TelnetBufferFull & TelnetBuffer
x = TelnetBufferFull &
TelnetProcess.StandardOutput.ReadToEnd
'MsgBox(x)
TextBox1.Text = x
TelnetProcess.Kill()
End Select

Catch ioe As InvalidOperationException
Catch ex As Exception
MsgBox(ex.ToString)
Finally
End Try

End Sub
--
Al
Systems Type


Apr 4 '06 #2
Al
Two points on that :

1. I would prefer not to use a commercial addin component as I have no
budget available for one.

2. Whilst in this instance I'm using PLink, it is likely that I will have
other external console/command-line programs to wrap and therefore I would
like to get the procedure sorted out properly now.

Al
Systems Type
"vg****@jscape.com" wrote:
Consider using Telnet Factory for .NET and SSH Factory for .NET
together to obtain both telnet and ssh scripting functionality:

<snip>
Apr 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: MackS | last post by:
Hi folks, Following FAQ 3.1, I am now interacting with the compiler interactively (in debug mode) by calling $ perl -de 1 However, I am not able to define a new subroutine for some repeated...
0
by: Patrick Fisher | last post by:
Hello I have just upgraded one of my programs from Acc97 to Acc2003 which was very easy process. The only problem I have found is that any form that has a Tab Control with forms on one or more...
0
by: Jonathan Wilson | last post by:
Firstly, to get msvcrt.lib, install the .NET framework SDK. The version of msvcrt.lib included there is the exact same one as comes with Visual Studio ..NET 2003. There are some other things that...
1
by: Peter Row | last post by:
Hi, BACKGROUND: I have a VB.NET DLL that uses HttpModules, HttpHandlers etc... which has been ported from a VB6 webclass application. I am developing on WinXP Pro SP1, P4 2Ghz, 512MB Ram I...
0
by: Tessa | last post by:
Is there any security reason why you cannot print to a network printer from ASP.NET under IIS6 on Windows 2003 server? I'm using ASP.NET code to print to a server print queue using...
0
by: nhaughton | last post by:
I have writtten a webservice in .Net 1.1 using C#, that exposes an existing COM+ application as a webservice to remote web applications. The COM+ application works fine under Win 2000, XP amd...
0
by: sukumaster | last post by:
hello everybody i hv created a tool bar by using macros in ms word 2003. i add some code for it in VB.i want to export that tool bar . because i have to send that tool bar to other sysems having...
2
by: Screaming Eagles 101 | last post by:
Hi, I looked and found a lot of different code regarding send/receiving input/output to a command window, but none helped me so far. I would like to start a process with telnet a bit like this...
1
by: hjazz | last post by:
Hi all, I'm having some problems with my program in VS .NET 2003. I initially wrote a module that uses the pthread library to create a number of threads to process something. This runs...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.