.Net使用SSH.NET通过SSH访问Linux主机

使用了SSH.NET库,添加引用dll至项目,以下代码显示了点击按钮后SSH链接Linux主机执行命令并返回命令执行结果

 protected void btnExcute_Click(object sender, EventArgs e)
{
string server = ServerAddr.Text;
string username = UserName.Text;
string password = Password.Text;
string command = Command.Text; StringBuilder result = new StringBuilder();
try
{
using (var client = new SshClient(server, username, password))
{
try
{
client.Connect();
result.Append(client.RunCommand(command).Execute() + "\r\n");
QAResult.Text = result.ToString();
client.Disconnect();
}
catch (Exception e1)
{
result.Append(e1.Message + "\r\n");
}
}
}
catch (Exception e1)
{
result.Append(e1.Message + "\r\n");
QAResult.Text = result.ToString();
}
}

以下代码演示了从本机拷贝文件到Linux主机

 private void TestTranferFiles()
{
string server = ServerAddr.Text;
string username = UserName.Text;
string password = Password.Text;
string command = Command.Text;
var scp = new ScpClient(server, username, password);
scp.Connect();
scp.Upload(new DirectoryInfo(@"d:\aaaa"), "/home/zy"); //将d:\\aaaa下的所有文件上传
scp.Disconnect();
}
上一篇:JAVA RPC (六) 之手把手从零教你写一个生产级RPC之client的代理


下一篇:Javascript的二进制数据处理学习 ——nodejs环境和浏览器环境分别分析