Run and execute multiple dependent SSH commands using C#

后端 未结 2 1303
-上瘾入骨i
-上瘾入骨i 2020-12-20 06:56

I want to change directory inside SSH using C# with SSH.NET library:

SshClient cSSH = new SshClient(\"192.168.80.21\", 22, \"appmi\", \"Appmi\");

cSSH.Conne         


        
2条回答
  •  Happy的楠姐
    2020-12-20 07:17

    this is what I have done and its working for me:

    SshClient sshClient = new SshClient("some IP", 22, "loign", "pwd");
    sshClient.Connect();
    
    ShellStream shellStream = sshClient.CreateShellStream("xterm", 80, 40, 80, 40, 1024);
    
    string cmd = "ls";
    shellStream.WriteLine(cmd + "; echo !");
    while (shellStream.Length == 0)
     Thread.Sleep(500);
    
    StringBuilder result = new StringBuilder();
    string line;
    
    string dbt = @"PuttyTest.txt";
    StreamWriter sw = new StreamWriter(dbt, append: true);           
    
     while ((line = shellStream.ReadLine()) != "!")
     {
      result.AppendLine(line);
      sw.WriteLine(line);
     }            
    
     sw.Close();
     sshClient.Disconnect();
     sshClient.Dispose();
     Console.ReadKey();
    

提交回复
热议问题