2012-05-19 2 views
1

이 질문에 대한 답변을 많이 모았 습니다만 시작하기 좋은 방법은 없습니다.C# - SSH Winforms 콘솔을 에뮬레이트하십시오.

것은 내가 sharpssh를 사용하고 있지만 콘솔 응용 프로그램을 위해 만들어졌다. 따라서 winforms 응용 프로그램을 만들려고 할 때 원하는대로 실행할 수 없습니다.

내 양식 : 출력을 표시하는 텍스트 상자 사용자가 명령을 작성해야하는 텍스트 상자.

임은 작성한 Readline에 붙어(), I는 사용자의 입력 한 후 텍스트 상자에 삽입 된 명령을 전송 명중 할 때까지 응용 프로그램을 일시 정지 할 필요가있다. WinForm 응용 프로그램으로 응용 프로그램을 변환하는 방법을 모릅니다.

그래서 질문은 이것에 대해 이동하는 방법은? - 응용 프로그램을 초기화 할 때 시작되는 프로세스와 출력이 수신되고 입력이 필요할 때 시작되는 프로세스 중 하나를 사용해야합니까? 그리고 두 번째 프로세스를 사용하여 텍스트 상자에 입력 된 이벤트 입력 키에 대한 입력 수신을 수집하고 입력을 보내고 출력을 다시 기다리는 다른 프로세스를 시작 하시겠습니까?

그렇다면 어떻게 해결할 수 있습니까?

다음 코드는 무엇입니까?

public Form1() 
    { 
     InitializeComponent(); 

     txthost.Text = "XXX"; 
     txtuser.Text = "XXXXX"; 
     txtpass.Text = "XXXXX"; 
     string pattern = "sdf:"; 
     mPattern = pattern; 
     this.txtInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(checkforenter); 
    } 

    public void button1_Click(object sender, EventArgs e) 
    { 

     try 
     { 

      mShell = new SshShell(Host, User); 
      mShell.Password = Pass; 
      //WRITING USER MESSAGE 
      txtOutput.AppendText("Connecting..."); 
      mShell.Connect(); 
      txtOutput.AppendText("OK"); 
      //txtOutput.AppendText("Enter a pattern to expect in response [e.g. '#', '$', C:\\\\.*>, etc...]: "); 
      //Stop for user input 

      mShell.ExpectPattern = mPattern; 
      mShell.RemoveTerminalEmulationCharacters = true; 
      _writer = new TextBoxStreamWriter(txtOutput); 

      Console.SetOut(_writer); 

      StringReader reader = new StringReader(txtInput.Text); 

      while (mShell.ShellOpened) 
      { 
       txtOutput.AppendText("\r\n" + "TERMINAL MODE ENGAGED"); 
       txtOutput.AppendText(mShell.Expect(pattern)); 

       txtInput.Text = Console.ReadLine(); 
       Console.ReadLine(); 
       Console.WriteLine(Environment.NewLine); 
       txtOutput.AppendText(mShell.Expect(pattern)); 
       MessageBox.Show("innan enter 2"); 
       Console.WriteLine(Environment.NewLine); 
       txtOutput.AppendText(mShell.Expect(("(continue)"))); 
       MessageBox.Show("efter enter 2"); 
       Console.WriteLine(Environment.NewLine); 
       //Data from termninal --> Append to text 
       string output = mShell.Expect(Pattern); 
       txtOutput.AppendText(output); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 

    } 

답변

4

는 CodeProject의 SharpSSH의 문서에 따르면 (http://www.codeproject.com/Articles/11966/sharpSsh-A-Secure-Shell-SSH-library-for-NET)을위한 두 가지 방법 SSH 연결을 읽고 쓸 수 있습니다 :

//Writing to the SSH channel 
ssh.Write(command); 

//Reading from the SSH channel 
string response = ssh.ReadResponse(); 

텍스트 상자가 콘솔이되어 더 이상 콘솔 개체를 사용할 필요가 없습니다. 입력 키를 눌렀을 때를 감지해야합니다. 이 키를 누르면 Write 명령을 실행하고 마지막으로 입력 한 내용을 SSH 연결로 보냅니다. 그런 다음 응용 프로그램을 일시 중지하고 응답을 기다리는 ReadResponse를 수행하십시오. 다시 돌아 오면 결과 문자열을 텍스트 상자에 추가 할 수 있습니다.

아마도 사용자가 입력 한 내용을 감지하고이를 SSH 연결로 보내는 것에 대해 걱정할 것입니다. 당신이 할 수있는 일은 ReadResponse를 할 때마다, 텍스트 상자에서 문자 색인을 얻은 다음 문자를 누른 후에 문자를 사용하는 것입니다. 을 입력하십시오.

또는 입력 용과 출력용의 두 개의 텍스트 상자를 사용할 수 있습니다.

희망이 도움이됩니다.

관련 문제