Quantcast
Channel: sshnet Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1729

New Post: Not able to redirect output to string

$
0
0
Hello All,

Based on various examples on this forum, I found a way to execute commands via SSH.NET
Example below.

using (var ssh = new SshClient(connectionInfo))
        {
            ssh.Connect();
            var cmd = ssh.CreateCommand("ls -l");   //  very long list
            var asynch = cmd.BeginExecute(delegate(IAsyncResult ar)
            {
                Console.WriteLine("Finished.");
            }, null);

            var reader = new StreamReader(cmd.OutputStream);

            while (!asynch.IsCompleted)
            {
                var result = reader.ReadToEnd();
                if (string.IsNullOrEmpty(result))
                    continue;
                Console.Write(result);
            }
            cmd.EndExecute(asynch);
        }
The problem i am running into is the fact that when I debug line by line I am able to return the output in a string variable, but when i run it all together, the return string variable does not have any value.

Any suggestions on how to get the values while running the entire code. The code is part of an automation which will be triggered based on some action and we cant have it debug line by line

Viewing all articles
Browse latest Browse all 1729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>