locked
Unable to capture plink.exe output using process.standardoutput RRS feed

  • Question

  • User1284979839 posted

    Hello Team, 

    I am trying execute remote command on a linux machine via Asp.net (vb.net), however unable to capture the output..

    I tried many diff ways like:

    1. Calling directly plink.exe from asp.net and then passing the argument

    2. escapsulating the plink.exe command inside a batch file and then calling that batch file from the asp.net 

    The same batch file is running fine when I am calling from CMD. Below provided is the code ( any help on this is highly appreciated ).. Here I am calling the Plink.exe directly.. 

    Protected Sub Button9_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles Button9.Click
    Dim Srv As String
    Label1.Text = ""
    Dim lineCount As Integer = 0
    Dim output1 As StringBuilder = New StringBuilder()
    
    Srv = TextBox9.Text
    
    Dim p As New Process()
    p.StartInfo.FileName = "D:\plink.exe"
    p.StartInfo.Arguments = " -ssh user1@someserver -pw <somepassword> -m D:\Unix_test.txt" p.StartInfo.UseShellExecute = False p.StartInfo.RedirectStandardOutput = True p.StartInfo.RedirectStandardError = True p.Start() Dim i = 1 While i < 200 output = output & p.StandardOutput.ReadLine() & "<br>" i = i + 1 End While p.WaitForExit() p.Close() Label1.Visible = True Label1.Text = output End Sub

    -m text file (i.e. unix_test.txt) has the below 

    df -k

    if I call from batch file then the batch file has the below line.. 

    D:\plink -l someusername -pw somepassword <ServerName> -m D:\unix_test.txt

    When I am running the same command using windows command prompt, I am getting the desired result. 

    A quick help is much appreciated.. 

    Thursday, November 16, 2017 5:26 PM

Answers

  • User-580799299 posted

    Hi,

    I have used logic like the following to successfully capture output from a FTP process that my web page started (in order to make sure the FTP commands did not have an error):

    Dim p As New Process
    Dim sw As New StringWriter
    p.StartInfo.RedirectStandardInput = True p.StartInfo.RedirectStandardOutput = True p.StartInfo.UseShellExecute = False p.StartInfo.FileName = "ftp" p.StartInfo.Arguments = "-n -s:upload_cmds.txt " & cfg.FTPHostName p.StartInfo.WorkingDirectory = fi.DirectoryName p.Start() Dim fw As StreamWriter = p.StandardInput Dim fr As StreamReader = p.StandardOutput While Not fr.EndOfStream sw.WriteLine("FtpPut: " & fr.ReadLine) End While sw.WriteLine() sw.WriteLine("The FTP process Completed Successfully, but check the lines starting with 'FtpPut:' for any errors reported by the FTP Host.") sw.WriteLine() fw.Close() fr.Close()

    This does not appear significantly different than your sample, but in my case when debugging the code, the fr.EndOfStream check seems to trigger the execution of the FTP commands.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 16, 2017 7:14 PM

All replies

  • User753101303 posted

    Hi,

    What if you try output=p.StandardOutput.ReadToEnd() rather than this kind of hardcoded loop?

    Also you tried to check the ExitCode. I would first make 100% sure it runs fine without the output redirection.

    Thursday, November 16, 2017 6:08 PM
  • User-580799299 posted

    Hi,

    I have used logic like the following to successfully capture output from a FTP process that my web page started (in order to make sure the FTP commands did not have an error):

    Dim p As New Process
    Dim sw As New StringWriter
    p.StartInfo.RedirectStandardInput = True p.StartInfo.RedirectStandardOutput = True p.StartInfo.UseShellExecute = False p.StartInfo.FileName = "ftp" p.StartInfo.Arguments = "-n -s:upload_cmds.txt " & cfg.FTPHostName p.StartInfo.WorkingDirectory = fi.DirectoryName p.Start() Dim fw As StreamWriter = p.StandardInput Dim fr As StreamReader = p.StandardOutput While Not fr.EndOfStream sw.WriteLine("FtpPut: " & fr.ReadLine) End While sw.WriteLine() sw.WriteLine("The FTP process Completed Successfully, but check the lines starting with 'FtpPut:' for any errors reported by the FTP Host.") sw.WriteLine() fw.Close() fr.Close()

    This does not appear significantly different than your sample, but in my case when debugging the code, the fr.EndOfStream check seems to trigger the execution of the FTP commands.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 16, 2017 7:14 PM
  • User1284979839 posted

    Thanks a lot Dean and sorry for the delayed response.. Your code worked for me with a little tweak.. 

    I decided to share the code for others to use it .. 

    Protected Sub Button9_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles Button9.Click
            Dim Srv As String
            Label1.Text = ""
            Dim lineCount As Integer = 0
            Dim output1 As StringBuilder = New StringBuilder()
    
            Srv = TextBox9.Text
    
            Dim p As New Process()
    
            Dim sw As New StringWriter
            p.StartInfo.RedirectStandardInput = True
            p.StartInfo.RedirectStandardOutput = True
            p.StartInfo.UseShellExecute = False
            p.StartInfo.FileName = "D:\plink.exe"
            p.StartInfo.Arguments = " -ssh someusr@" & Srv & " -pw somepassword -m D:\Unix_test.txt"
            p.StartInfo.WorkingDirectory = "D:\"
            p.Start()
            Dim fw As StreamWriter = p.StandardInput
            Dim fr As StreamReader = p.StandardOutput
    
            While Not fr.EndOfStream
                output = output + fr.ReadLine + "<br>"
            End While
            
            Label1.Visible = True
            Label1.Text = "<br>" + output
    
            fw.Close()
            fr.Close()
            p.WaitForExit()
            p.Close()
            
        End Sub
    Monday, November 20, 2017 10:27 AM