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

New Post: Running commands not returning results

$
0
0
exits not exists. I'm pretty sure cat exists on every system out there :)

Please check the connection state when the ReadToEnd() hangs. I think I can trigger your problem with this:
            while (true)
            {

                var sshCommand = client.CreateCommand("cat /etc/*-release");
                var asynch = sshCommand.BeginExecute();

                while (!asynch.IsCompleted)
                {
                    Console.WriteLine("waiting");
                    Thread.Sleep(10);
                }
                client.Disconnect();

                sshCommand.EndExecute(asynch);
                Console.WriteLine("Date: " + DateTime.Now);
                Console.WriteLine("StdErr: " + sshCommand.Error);
                Console.WriteLine("StdOut: " + sshCommand.Result);
                Console.WriteLine("ExitCode: " + sshCommand.ExitStatus);

                Thread.Sleep(1000);
            }
=> I get one Output, the next one hangs.

Yes, I should have written SshCommand, not RunCommand().


Here is simple example of ShellStream with async read:
        private static byte[] _data = new byte[2048];
        private static ShellStream stream;
        private static SshClient client;

        private static void Main()
        {
            client = new SshClient("test", "test", "test");
            client.Connect();

            stream = client.CreateShellStream(@"xterm", 80, 24, 800, 600, 1024);
            stream.DataReceived += StartAsyncRead;
            stream.Write("env | grep TERM\n");
            stream.Write("echo $TERM\n");

            Console.ReadLine();
        }

        private static void StartAsyncRead(object sender, EventArgs e)
        {
            try
            {
                stream.BeginRead(_data, 0, _data.Length, OnReadCompletion, new MyAsyncInfo(_data, stream));
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }

        private static void OnReadCompletion(IAsyncResult ar)
        {
            try
            {
                var mai = (MyAsyncInfo) ar.AsyncState;
                int datalen = mai.Stream.EndRead(ar);
                string line = client.ConnectionInfo.Encoding.GetString(mai.ByteArray, 0, datalen);
                Console.Write(line);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
    }

    public class MyAsyncInfo
    {
        public MyAsyncInfo(Byte[] array, ShellStream stream)
        {
            ByteArray = array;
            Stream = stream;
        }

        public Byte[] ByteArray { get; set; }
        public ShellStream Stream { get; set; }
    }

Viewing all articles
Browse latest Browse all 1729

Trending Articles



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