C# 发送一个简单的 SSH 命令 [英] C# send a simple SSH command

查看:114
本文介绍了C# 发送一个简单的 SSH 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名年轻的学生,我收到了本周的作业.这对我来说非常困难,因为我必须创建一个程序,该程序能够连接到 SSH 服务器并发送命令etc/init.d/networking restart".

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".

我应该用 C# 编写这个应用程序,我对它很陌生(刚刚从一些学校课程中学到了东西).我还应该创建一个 GUI.

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# 的基础知识(循环、if 等...).

I understand the basics of C# (loop, if etc...).

我已经创建了 GUI、菜单、按钮和日志列表框.GUI = 3 个文本框(ip、用户名、密码)和一个用于连接的按钮.

I've already created GUI, menus, buttons and a log listbox. GUI = 3 textboxes (ip, username, password) and a button to connect.

我正在使用 Microsoft Visual Studio 进行编码.

I'm coding with Microsoft Vistual Studio.

我的问题是:如何与我的服务器建立 SSH 连接?

My question is: How can I establish an SSH connection to my server?

推荐答案

我使用了 SSH.Net 不久前在一个项目中,对此非常满意.它还附带了一个很好的文档,其中包含大量有关如何使用它的示例.

I used SSH.Net in 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.

仍然可以在此处找到原始软件包网站,包括文档(目前在GitHub).

The original package website can be still found here, including the documentation (which currently isn't available on 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();
}

这篇关于C# 发送一个简单的 SSH 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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