예제 #1
0
        private async void TabPageRTBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                temp_str = TabPageRTBox.Text.Substring(last_pos).Trim();
                Console.WriteLine($"Command TabPageRTBox_KeyDown is {temp_str}");
                // write to the Stream from begining every time.
                // Write the length first as int (4 bytes)
                //mstream.Position = 0;
                byte[] cmd_bytes = Encoding.Unicode.GetBytes(temp_str);

                byte[] cmd_len     = BitConverter.GetBytes(cmd_bytes.Length);
                int    cmd_len_int = cmd_bytes.Length;
                int    len_of_int  = cmd_len.Length; // usually four bytes on 64 bit systems, sizeof int

                /*
                 * mstream.Write(cmd_len, 0, len_of_int);
                 * int count = 0;
                 * while (count < cmd_len_int)
                 * {
                 *  mstream.WriteByte(cmd_bytes[count++]);
                 * }
                 */
                //mstream.WriteAsync(temp_str);
                await tabProc.WriteToProcessAsync(temp_str + "\n");

                Console.WriteLine($"Written to stream........{temp_str}");

                Console.WriteLine("Readming from stream........");
                string outstr = await tabProc.ReadFromProcessAsync();

                Console.WriteLine($"Line........:{outstr}");
                outstr = await tabProc.ReadFromProcessAsync();

                Console.WriteLine($"Line........:{outstr}");

                this.AppendText("\n");

                TabPageRTBox.DeselectAll();
            }
            else
            {
            }

            /*
             * else
             * {
             *  if(e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Capital)
             *  {
             *      shift_pressed = true;
             *  }
             *  else
             *  {
             *      int kval = e.KeyValue;
             *      if (!shift_pressed)
             *      {
             *          kval += 32;
             *      }
             *      temp_str += (char)kval;
             *  }
             *
             * }
             */
        }
예제 #2
0
파일: Form1.cs 프로젝트: MadNit/VTab
        public async Task RunProcessAsync(string ansicon_cmd, string plink_cmd)
        {
            // Execute plink.exe
            // Exit on error and close the tab
            // Else stay on the tab, wait for the user input
            // and return output.
            // Create a tab
            VCustomTab myTabPage = new VCustomTab("");

            try
            {
                using (Process myProcess = new Process())
                {
                    String macname = macTxtBox.Text;
                    String usrname = usrTxtBox.Text;
                    String pwd     = pwdTxtBox.Text;
                    //String args = plink_cmd + " -l " + usrname + " -pw " + pwd + " " + macname;
                    String           args      = $"{plink_cmd} -l {usrname} -pw {pwd} {macname}";
                    ProcessStartInfo startInfo = new ProcessStartInfo(ansicon_cmd, args);
                    startInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                    startInfo.UseShellExecute        = false;
                    startInfo.CreateNoWindow         = true;
                    startInfo.RedirectStandardOutput = true;
                    startInfo.RedirectStandardError  = true;
                    startInfo.RedirectStandardInput  = true;


                    // plink.exe is the cmd tool for putty.
                    myProcess.StartInfo = startInfo;
                    //myProcess.OutputDataReceived += CaptureOutput;
                    //myProcess.ErrorDataReceived += CaptureError;
                    // This code assumes the process you are starting will terminate itself.
                    // Given that is is started without a window so you cannot terminate it
                    // on the desktop, it must terminate itself or you can do it programmatically
                    // from this application using the Kill method.


                    myProcess.OutputDataReceived += ProcessOutputHandler;
                    String outstr = "vj";
                    myProcess.Start();

                    tabProc = new TabProcess(myProcess.StandardInput, myProcess.StandardOutput);

                    StreamWriter process_std_input_stream = myProcess.StandardInput;
                    String       tempStr = "";
                    //outstr = await ReadFromProcessAsync(myProcess.StandardOutput);
                    outstr = await tabProc.ReadFromProcessAsync();

                    tempStr += outstr;
                    //outstr = await ReadFromProcessAsync(myProcess.StandardOutput);
                    outstr = await tabProc.ReadFromProcessAsync();

                    tempStr += outstr;

                    string nextcmd = "\n"; // Enter after success
                    //await WriteToProcessAsync(myProcess.StandardInput, nextcmd);
                    await tabProc.WriteToProcessAsync(nextcmd);

                    ANSIEscapeSeq ansi_seq = new ANSIEscapeSeq();
                    // TODO: The end with character may differ : Taken Care, ANSIEscapeSeq class
                    while (outstr != null && !outstr.Trim().EndsWith("$"))
                    {
                        tempStr += outstr + "\n";
                        //outstr = await ReadFromProcessAsync(myProcess.StandardOutput);
                        outstr = await tabProc.ReadFromProcessAsync();

                        /*
                         * byte[] escTitleBytesRec = Encoding.ASCII.GetBytes(outstr);
                         * foreach (byte b in escTitleBytesRec)
                         *  Console.Write((char)b + "( " + b + ") ");
                         */
                        //myTabPage.WriteLine(outstr + "\n");
                    }
                    // Get the command prompt and title now.
                    var out_tup = ansi_seq.getTitleAndCommandPrompt(outstr);
                    myTabPage.setTitle(out_tup.Item1);
                    myTabPage.setPrompt(out_tup.Item2);
                    myTabPage.setProcessTab(tabProc);
                    tabControl1.TabPages.Add(myTabPage);

                    myTabPage.WriteLine(tempStr);
                    myTabPage.WriteLine(out_tup.Item2);

                    // Loop until the command is logout or exit or on error.

                    //await ReadFromMemStreamAsync();
                    //outstr = await ReadFromProcessAsync(myProcess.StandardOutput);
                    Console.WriteLine($"Form1: The output we got is: {outstr}");

                    /*
                     * try
                     * {
                     *  Thread.Sleep(10000);
                     *  //nextcmd = "logout";
                     *  nextcmd = "";
                     *  await WriteToProcessAsync(myProcess.StandardInput, nextcmd);
                     * }
                     * catch (Exception eex)
                     * {
                     *  Console.WriteLine($"An error occurred {eex.Message}");  // The $ is the fstrings in python.
                     * }
                     *
                     * if (myProcess.HasExited)
                     * {
                     *  myProcess.Close();
                     *  myProcess.Dispose();
                     * }
                     * else
                     * {
                     *  myProcess.Kill();
                     * }
                     */
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }