Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Command Run remote System

I have to Run a command in Unix System from my C# Application running on Windows. The two system is in same network and I have all the required credentials.

Is there is any API from which I can run "ls" command of UNIX from C# code by establishing a SSH connection.

EDIT: I am looking for a solution which will help in running any command or script present in Remote System.

like image 897
vrbilgi Avatar asked Jan 03 '11 09:01

vrbilgi


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

What is C language?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.


1 Answers

Systems that run SSH usually support some kind of SFTP, so you could just use something like SSH.NET:

using (var sftpClient = new SftpClient("localhost", "root", "bugmenot")
{
    sftpClient.Connect();
    var files = sftpClient.ListDirectory("/tmp");
}

or SharpSSH:

Sftp sftp = new Sftp("localhost", "root", "bugmenot");
try
{
    sftp.Connect();
    ArrayList files = sftp.GetFileList("/tmp");
}
finally
{
    sftp.Close();
}

Edit: You can run any command over SSH with both libraries. Admittedly, I have not done that, yet, but it is supposed to work like this:

SSH.NET

using (var sshClient = new SshClient("localhost", "root", "bugmenot")
{
    sshClient.Connect();
    var cmd = sshClient.RunCommand("ls");
    var output = cmd.Result;
}

SharpSSH

SshStream ssh = new SshStream("localhost", "root", "bugmenot");
try
{
    ssh.Write("ls");
    var output = ssh.ReadResponse();
}
finally
{
    ssh.Close();
}
like image 143
hangy Avatar answered Oct 18 '22 20:10

hangy