Menu

#2 Bug in SshExec.RunCommand, duplicate data

open
ClemDogU
None
5
2008-01-31
2007-08-02
Anonymous
No

Description:
The function RunCommand(cmd, stdout, stderr) duplicates output when output retrieved exceeds the 1k boundry (class SshExec).

Problem:
The output is assigned using the =+ operator after the string builder is used to append the read buffer.

Solution:
Assign the output after reading all the data using the string builder ToString function.

Code:

public int RunCommand(string command, ref string StdOut, ref string StdErr)
{
StdOut = "";
StdErr = "";
m_channel = GetChannelExec(command);
System.IO.Stream stdout = m_channel.getInputStream();
System.IO.Stream stderr = ((ChannelExec)m_channel).getErrStream();
m_channel.connect();
byte[] buff = new byte[1024];
StringBuilder sbStdOut = new StringBuilder();
StringBuilder sbStdErr = new StringBuilder();
int o=0; int e=0;
while(true)
{
if(o!=-1) o = stdout.Read(buff, 0, buff.Length);
if(o!=-1) sbStdOut.Append(Encoding.ASCII.GetString(buff, 0, o));
if(e!=-1) e = stderr.Read(buff, 0, buff.Length);
if(e!=-1) sbStdErr.Append(Encoding.ASCII.GetString(buff, 0, e));
if((o==-1)&&(e==-1)) break;
}
m_channel.disconnect();
StdOut = sbStdOut.ToString();
StdErr = sbStdErr.ToString();

return m_channel.getExitStatus();
}

Author:
Ronald Bosscher
Ronald.Bosscher@capgemini.com

Discussion

  • ClemDogU

    ClemDogU - 2008-01-31
    • assigned_to: nobody --> clemdogu
     
  • ClemDogU

    ClemDogU - 2008-02-09

    Logged In: YES
    user_id=1114878
    Originator: NO

    I reviewed the code above and place it in CVS giving credit to Ronald Bosscher. I do appreciate the debugging by our users. Thank you Ronald. Currently you should be able to pull the SshExe.cs file from CVS and recompile the dll.

     
  • Tony Gravagno

    Tony Gravagno - 2011-01-21

    I can verify this issue and confirm the solution. I just noticed that I was getting chunks of data repeated in StdOut. This was corrected with the code here.

    I wish other sites had some reference back to SF and to the latest code. From the outside this great project is looking stale and unmaintained. I guess it's time to get new source from CVS.

    Thanks to Ronald Bosscher and clemdogu.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.