在C#中如何使用TcpClient在远程SSH上运行Unix命令 [英] In C# how to run unix command on remote SSH using TcpClient

查看:63
本文介绍了在C#中如何使用TcpClient在远程SSH上运行Unix命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从C#应用程序在远程主机上运行UNIX命令.

I am trying to run a UNIX command on remote host from my C# application.

我已经看到了互联网上的各种帖子,包括使用诸如SmartSsh之类的各种其他许多库来进行此操作的SO.还有一些使用命令行命令.

I have seen various post over internet including SO for doing this using various libraries like SmartSsh any many others. And some others using command line commands.

我不想使用任何第三方库.

I do not want to use any third party library.

因此,我尝试使用TcpClient类,并在 MSDN .我将主机名传递为: user @ servername 和端口 22 .当我运行此代码时,它将引发异常

Hence I tried using TcpClient class with example shown here on MSDN. I am passing hostname as : user@servername and port 22. When I run this code it trows an exception

SocketException (0x80004005): No such host is known
at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port) 

是因为我的服务器/主机要求用户输入密码,如果是,那么我该如何提供呢?

Is it because my server/host expect a password for user, if yes then how do I provide that?

推荐答案

套接字仅与连接有关,将其视为通过电话呼叫某人.套接字确保您与其他人有联系,但是您仍然需要一种语言(协议)来进行通信.通过电话,这是一种通用语言.当您要执行Unix命令时,您还需要您和目标用户都可以理解的语言.其中一种协议是SSH,端口22.SSH还定义了身份验证,因此您需要实现SSH协议或更好的协议,请使用第3方库为您完成此操作

A socket is only about a connection, see it as calling a person over the phone. The socket makes sure you have a conncetion with the other person but you still need a language (protocol) to communicate. Over the phone, that is a common language. When you want to execute unix commands you also need a language that both the you and the target understand. One of those protocols is SSH, port 22. SSH also defines authentication, so you need to implement the SSH protocol or better, use a 3th party library to do that for you

这篇关于在C#中如何使用TcpClient在远程SSH上运行Unix命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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