使用C#运行和执行多个从属SSH命令 [英] Run and execute multiple dependent SSH commands using C#

查看:296
本文介绍了使用C#运行和执行多个从属SSH命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有SSH.NET库的C#在SSH内部更改目录:

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.Connect();

Console.WriteLine("current directory:");
Console.WriteLine(cSSH.CreateCommand("pwd").Execute());

Console.WriteLine("change directory");
Console.WriteLine(cSSH.CreateCommand("cdr abc-log").Execute());

Console.WriteLine("show directory");
Console.WriteLine(cSSH.CreateCommand("pwd").Execute());

cSSH.Disconnect();
cSSH.Dispose();

Console.ReadKey();

但是它不起作用.我还检查了以下内容:

But it's not working. I have also checked below:

Console.WriteLine(cSSH.RunCommand("cdr abc-log").Execute());

但仍然无法正常工作.

推荐答案

这是我已经完成的工作,并且对我有用:

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();

这篇关于使用C#运行和执行多个从属SSH命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆