C# 发送一个简单的 SSH 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11169396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# send a simple SSH command
提问by lucbas
I'm a young a student and received my homework for this week. It's pretty difficult for me because I have to create a program, that is able to connect to an SSH server and send the command "etc/init.d/networking restart".
我是一名年轻的学生,本周收到了我的作业。这对我来说非常困难,因为我必须创建一个程序,该程序能够连接到 SSH 服务器并发送命令“etc/init.d/networking restart”。
I should program this application in C# and I'm pretty new to it (Just have learned from some school lessons). I also should create a GUI.
我应该用 C# 编写这个应用程序,我对它很陌生(刚刚从一些学校课程中学到了东西)。我还应该创建一个 GUI。
I understand the basics of C# (loop, if etc...).
我了解 C# 的基础知识(循环、if 等...)。
I've already created GUI, menus, buttons and a log listbox. GUI = 3 textboxes (ip, username, password) and a button to connect.
我已经创建了 GUI、菜单、按钮和日志列表框。GUI = 3 个文本框(ip、用户名、密码)和一个用于连接的按钮。




I'm coding with Microsoft Vistual Studio.
我正在使用 Microsoft Visual Studio 进行编码。
My question is: How can I establish an SSH connection to my server?
我的问题是:如何与我的服务器建立 SSH 连接?
采纳答案by Karl-Johan Sj?gren
I used SSH.Netin a project a while ago and was very happy with it. It also comes with a good documentation with lots of samples on how to use it.
不久前我在一个项目中使用了SSH.Net并且对它非常满意。它还附带了一个很好的文档,其中包含许多有关如何使用它的示例。
The original package website can be still found here, including the documentation (which currently isn't available on GitHub).
仍然可以在此处找到原始软件包网站,包括文档(目前在 GitHub 上不可用)。
For your case the code would be something like this.
对于您的情况,代码将是这样的。
using (var client = new SshClient("hostnameOrIp", "username", "password"))
{
client.Connect();
client.RunCommand("etc/init.d/networking restart");
client.Disconnect();
}
回答by NickD
SharpSSH should do the job. http://www.codeproject.com/Articles/11966/sharpSsh-A-Secure-Shell-SSH-library-for-NET
SharpSSH 应该可以完成这项工作。 http://www.codeproject.com/Articles/11966/sharpSsh-A-Secure-Shell-SSH-library-for-NET
回答by Mario Palacios
SshClient cSSH = new SshClient("192.168.10.144", 22, "root", "pacaritambo");
cSSH.Connect();
SshCommand x = cSSH.RunCommand("exec \"/var/lib/asterisk/bin/retrieve_conf\"");
cSSH.Disconnect();
cSSH.Dispose();
//using SSH.Net
//使用SSH.Net

