winform下调用SharpSSH库实现对远程linux主机的控制
2017-07-13 11:57
701 查看
本教程在虚拟机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:
注意这段代码需要更改自己linux主机的用户名密码,并添加dll引用。
代码段2:
首先下载源码: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) { } } }
相关文章推荐
- tightvnc实现windows远程连接控制linux主机
- Linux系统中如何实现远程控制
- Putty实现自动登陆远程Linux主机的方法
- C#调用RDP,实现远程桌面共享及控制
- 使用kernel编译+busybox定制Linux系统--实现ssh远程登录+web服务的迷你主机
- bobo教你如何通过putty实现远程登录控制linux平台
- nodejs+MQTT协议实现远程主机控制
- Esp8266进阶之路12 图文并茂学习阿里云主机搭建8266MQTT服务器,实现移动网络远程控制一盏灯!
- 【Linux学习笔记】Linux主机如何实现无需输入密码的远程登录或脚本远程执行
- 用VNC实现Linux操作系统中的远程控制
- c#调用TeamViewer或AnyDesk实现远程控制
- linux主机上编译安装rpcapd实现wireshark远程抓包功能
- Python 使用paramiko实现远程登录Linux主机并执行命令和操作
- ssh实现Linux主机间的远程文件传输
- Linux系统桌面下实现远程控制win7
- Linux系统中如何实现远程控制
- linux expect实现登陆远程主机并执行命令示例代码
- Linux运维笔记-文档总结-Apache基本配置的介绍和语言支持,虚拟主机,访问控制的实现
- Putty实现自动登陆远程Linux主机的方法
- java实现远程调用linux命令或脚本