我必须从在 Windows 上运行的 C# 应用程序在 Unix 系统中运行命令。 这两个系统在同一个网络中,我拥有所有必需的凭据。
是否有任何 API 可以让我从 C# 代码运行 UNIX 的“ls”命令 建立 SSH 连接。
编辑: 我正在寻找一个有助于运行任何命令或脚本的解决方案 在远程系统中。
最佳答案
运行 SSH 的系统通常支持某种 SFTP,因此您可以只使用类似 SSH.NET 的东西:
using (var sftpClient = new SftpClient("localhost", "root", "bugmenot")
{
sftpClient.Connect();
var files = sftpClient.ListDirectory("/tmp");
}
或SharpSSH :
Sftp sftp = new Sftp("localhost", "root", "bugmenot");
try
{
sftp.Connect();
ArrayList files = sftp.GetFileList("/tmp");
}
finally
{
sftp.Close();
}
编辑:您可以使用这两个库通过 SSH 运行任何命令。诚然,我还没有这样做,但它应该是这样工作的:
using (var sshClient = new SshClient("localhost", "root", "bugmenot")
{
sshClient.Connect();
var cmd = sshClient.RunCommand("ls");
var output = cmd.Result;
}
SshStream ssh = new SshStream("localhost", "root", "bugmenot");
try
{
ssh.Write("ls");
var output = ssh.ReadResponse();
}
finally
{
ssh.Close();
}
关于C#命令运行远程系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4583358/