In C# how to run unix command on remote SSH using TcpClient

心不动则不痛 提交于 2020-01-21 12:45:16

问题


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

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.

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?


回答1:


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



来源:https://stackoverflow.com/questions/8516284/in-c-sharp-how-to-run-unix-command-on-remote-ssh-using-tcpclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!