winform下调用SharpSSH库实现对远程linux主机的控制
本教程在虚拟机XP+VS2010下测试成功。首先下载源码:SharpSSH-1.1.1.13.src.zip。然后用vs2010打开项目,右键SharpSSH(不是Example)点击生成,然后在SharpSSH-1.1.1.13.src\SharpSSH\bin\Release或者(Debug)目录下生成有三个DLL文件。自己用C#调用SharpSSH库实现连接主机功能简单代码示例如下
·
本教程在虚拟机XP+VS2010下测试成功。
首先下载源码:SharpSSH-1.1.1.13.src.zip。
然后用vs2010打开项目,右键SharpSSH(不是Example)点击生成,然后在SharpSSH-1.1.1.13.src\SharpSSH\bin\Release或者(Debug)目录下生成有三个DLL文件。
自己用C#调用SharpSSH库实现连接主机功能简单代码示例如下:
代码段1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tamir.SharpSsh;
namespace SSHTest
{
class Program
{
static void Main(string[] args)
{
try
{
string host = "192.168.8.8";
string user = "test";
string pass = "1234";
Console.WriteLine("主机地址: {0}", host);
Console.WriteLine("登陆用户: {0}", user);
Console.WriteLine("登录密码: {0}", pass);
SshShell shell = new SshShell(host, user);
shell.Password = pass;
shell.RedirectToConsole();
Console.Write("正在连接...");
shell.Connect();
Console.WriteLine("连接完毕!");
Console.WriteLine("=========");
while (shell.ShellOpened)
{
System.Threading.Thread.Sleep(500);
}
Console.WriteLine("=========");
Console.WriteLine("断开连接中...");
shell.Close();
Console.WriteLine("断开完毕");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Write("按任意键继续...");
Console.ReadKey();
Console.WriteLine("\b");
Environment.Exit(0);
}
}
}
注意这段代码需要更改自己linux主机的用户名密码,并添加dll引用。
代码段2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tamir.SharpSsh;
using Tamir.SharpSsh.jsch;
using Org.Mentalis.Security;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
string host = "192.168.8.8";
string user = "test";
string pass = "8888";
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
ShellUserInfo ui = new ShellUserInfo();
ui.setPassword(pass);
session.setUserInfo(ui);
Console.Write("正在连接...");
session.connect();
//Open a new Shell channel on the SSH session
Channel channel = session.openChannel("shell");
//Redirect standard I/O to the SSH channel
channel.setInputStream(Console.OpenStandardInput());
channel.setOutputStream(Console.OpenStandardOutput());
//Connect the channel
channel.connect();
Console.WriteLine("-- Shell channel is connected using the {0} cipher", session.getCipher());
//Wait till channel is closed
while (!channel.isClosed())
{
System.Threading.Thread.Sleep(500);
}
//Disconnect from remote server
channel.disconnect();
session.disconnect();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
public class ShellUserInfo : UserInfo
{
String passwd;
public String getPassword() { return passwd; }
public void setPassword(String passwd) { this.passwd = passwd; }
public String getPassphrase() { return null; }
public bool promptPassphrase(String message) { return true; }
public bool promptPassword(String message) { return true; }
public bool promptYesNo(String message) { return true; }
public void showMessage(String message) { }
}
}
更多推荐
已为社区贡献9条内容
所有评论(0)