Example #1
0
        /// <summary>
        /// 控制台控制linux
        /// </summary>
        /// <param name="client"></param>
        /// <param name="commandStr"></param>
        /// <param name="successCallback"></param>
        /// <param name="errorCallback"></param>
        private void ExcuteCommand(SshClient client, string commandStr, Action <string> successCallback, Action <string> errorCallback)
        {
            try
            {
                var inStream     = new Renci.SshNet.Common.PipeStream();
                var outStream    = new MemoryStream();
                var exStream     = new MemoryStream();
                var shell        = client.CreateShell(inStream, outStream, exStream);
                var streamWriter = new StreamWriter(inStream)
                {
                    AutoFlush = true
                };
                shell.Started += (sender, args) =>
                {
                    while (true)
                    {
                        var command = Console.ReadLine();
                        streamWriter.WriteLine(command);
                        streamWriter.Flush();
                    }
                };
                int outIndex = 0;
                var td       = new Thread(() =>
                {
                    while (true)
                    {
                        var length = outStream.Length == 0 ? 0 : outStream.Length;
                        if (length - outIndex == 0)
                        {
                            continue;
                        }
                        byte[] buffer      = new byte[length - outIndex];
                        outStream.Position = outIndex;
                        outStream.Read(buffer, 0, buffer.Length);
                        outIndex = (int)length;
                        Console.WriteLine(Encoding.UTF8.GetString(buffer));
                        Thread.Sleep(1000);
                    }
                });
                td.Start();
                shell.Start();



                //client.ErrorOccurred += (sender, eventArgs) =>
                //{
                //    Console.WriteLine(eventArgs.Exception);
                //};
                //client.HostKeyReceived += (sender, eventArgs) =>
                //{
                //    Console.WriteLine(eventArgs.HostKeyName);
                //};

                //var command = client.CreateCommand(commandStr.ToString());
                //command.CommandTimeout = new TimeSpan(0, 0, 10);
                //command.Execute(commandStr);


                //if (!string.IsNullOrWhiteSpace(command.Error))
                //{
                //    errorCallback(command.Error);
                //    //result.Append(command.Error + "\r\n");
                //}
                //else
                //{
                //    successCallback(command.Result);
                //}
            }
            catch (Exception e1)
            {
                errorCallback(e1.Message);
            }
        }
Example #2
0
 public void InstallLiveService(Action <int, string> resultCallback, bool repair)
 {
     if (IsInstalledService() && !repair)
     {
         return;
     }
     using (SshClient client = new SshClient(_host, _port, _username, _password))
     {
         client.Connect();
         StringBuilder installLogBuilder = new StringBuilder();
         try
         {
             var inStream     = new Renci.SshNet.Common.PipeStream();
             var outStream    = new MemoryStream();
             var exStream     = new MemoryStream();
             var shell        = client.CreateShell(inStream, outStream, exStream);
             var streamWriter = new StreamWriter(inStream)
             {
                 AutoFlush = true
             };
             var daemonThread = new Thread(() =>
             {
                 int outIndex = 0;
                 while (true)
                 {
                     Thread.Sleep(200);
                     var length = outStream.Length == 0 ? 0 : outStream.Length;
                     if (length - outIndex == 0)
                     {
                         continue;
                     }
                     byte[] buffer      = new byte[length - outIndex];
                     outStream.Position = outIndex;
                     outStream.Read(buffer, 0, buffer.Length);
                     outIndex   = (int)length;
                     var result = Encoding.UTF8.GetString(buffer);
                     installLogBuilder.AppendLine(result);
                     Console.WriteLine(result);
                     if (result.Contains("Live Stream Service Installed!"))
                     {
                         shell.Stop();
                         shell.Dispose();
                         break;
                     }
                 }
                 resultCallback.Invoke(0, "Install Success.");
                 Thread.CurrentThread.Abort();
             });
             shell.Starting += (state, arg) =>
             {
             };
             shell.ErrorOccurred += (state, arg) =>
             {
             };
             shell.Stopped += (state, arg) =>
             {
             };
             shell.Stopping += (state, arg) =>
             {
             };
             shell.Started += (sender, args) =>
             {
                 daemonThread.Start();
                 Thread.Sleep(5000);
                 var sb = new StringBuilder();
                 sb.AppendLine(@"echo Start Stream Service Installed!");
                 sb.AppendLine("yum install -y autoconf automake cmake freetype-devel gcc gcc-c++ git libtool make mercurial nasm pkgconfig zlib - devel");
                 sb.AppendLine("yum update -y nss curl libcurl");
                 sb.AppendLine("git clone https://github.com/ossrs/srs");
                 sb.AppendLine("cd ~/srs/trunk");
                 sb.AppendLine("./configure --full&& make");
                 sb.AppendLine(@"echo Live Stream Service Installed!");
                 streamWriter.WriteLine(sb);
                 streamWriter.Flush();
                 Thread.CurrentThread.Join();
             };
             shell.Start();
         }
         catch (Exception e)
         {
             resultCallback(-1, e.Message);
             client.Disconnect();
             client.Dispose();
             throw;
         }
         finally
         {
             //todo: save installLogBuilder
         }
     }
 }