开源Granados介绍 - SSH连接远程Linux服务器(C#)

Granados是一个基于.NET的SSH客户端库。它有以下特点:

1.Granados是一个C#的开源项目。源码地址:http://www.routrek.co.jp/support/download/varaterm/granados200.tar.gz

2.同时支持SSH1和SSH2。

3.Granados实现了AES, Blowfish, TripleDES, RSA, DSA等加密验证算法。

4.实现TCP协议连接。

如何使用Granados库

可惜的是Granados的文档几乎没有!所以只有从它的源码找到它的测试代码来看。总结步骤为:

1.工程中添加Routrek.granados.dll(下载的包里有)的引用。

2.添加Reader类,实现ISSHConnectionEventReceiver和ISSHChannelEventReceiver接口。首先引用命名空间:

using System.Threading; using System.Diagnostics; using System.Net; using System.Net.Sockets; using Routrek.Crypto; using Routrek.SSHC; using Routrek.SSHCV1; using Routrek.SSHCV2; using Routrek.Toolkit; using Routrek.PKI;

Reader类实现如下:

class Reader : ISSHConnectionEventReceiver, ISSHChannelEventReceiver ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)    { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public SSHConnection _conn; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public bool _ready; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnData(byte[] data, int offset, int length) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            System.Console.Write(Encoding.ASCII.GetString(data, offset, length)); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnDebugMessage(bool always_display, byte[] data) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Debug.WriteLine("DEBUG: " + Encoding.ASCII.GetString(data)); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnIgnoreMessage(byte[] data) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Debug.WriteLine("Ignore: " + Encoding.ASCII.GetString(data)); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnAuthenticationPrompt(string[] msg) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Debug.WriteLine("Auth Prompt " + msg[0]); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnError(Exception error, string msg) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Debug.WriteLine("ERROR: " + msg); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnChannelClosed() ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Debug.WriteLine("Channel closed"); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            _conn.Disconnect(""); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            //_conn.AsyncReceive(this); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnChannelEOF() ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            _pf.Close(); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Debug.WriteLine("Channel EOF"); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnExtendedData(int type, byte[] data) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Debug.WriteLine("EXTENDED DATA"); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnConnectionClosed() ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Debug.WriteLine("Connection closed"); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnUnknownMessage(byte type, byte[] data) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Debug.WriteLine("Unknown Message " + type); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnChannelReady() ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            _ready = true; ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnChannelError(Exception error, string msg) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Debug.WriteLine("Channel ERROR: " + msg); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void OnMiscPacket(byte type, byte[] data, int offset, int length) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public PortForwardingCheckResult CheckPortForwardingRequest(string host, int port, string originator_host, int originator_port) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            PortForwardingCheckResult r = new PortForwardingCheckResult(); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            r.allowed = true; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            r.channel = this; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            return r; ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public void EstablishPortforwarding(ISSHChannelEventReceiver rec, SSHChannel channel) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            _pf = channel; ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        public SSHChannel _pf; ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)    }

3.好的,现在来测试一下:

class Program ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)    { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        private static SSHConnection _conn; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        static void Main(string[] args) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)        { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            SSHConnectionParameter f = new SSHConnectionParameter(); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            f.UserName = "root"; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            f.Password = "****"; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            f.Protocol = SSHProtocol.SSH2; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            f.AuthenticationType = AuthenticationType.Password; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            f.WindowSize = 0x1000; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Reader reader = new Reader(); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            s.Connect(new IPEndPoint(IPAddress.Parse("192.168.x.x"), 22)); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            _conn = SSHConnection.Connect(f, reader, s); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            reader._conn = _conn; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            SSHChannel ch = _conn.OpenShell(reader); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            reader._pf = ch; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            SSHConnectionInfo ci = _conn.ConnectionInfo; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            Thread.Sleep(1000); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)             ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            byte[] b = new byte[1]; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)            while (true) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)            { ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                int input = System.Console.Read(); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                b[0= (byte)input; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)                reader._pf.Transmit(b); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)            } ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)        } ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)    }

4.执行效果如下:

5.如果你需要快速的执行某些指定的命令,则可以把上面的

byte[] b = new byte[1];   while (true) ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif){ ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)      int input = System.Console.Read(); ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)      b[0= (byte)input; ![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)      reader._pf.Transmit(b); ![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif) }

替换为:

string cmd = "vi xxx.txt\n"; byte[] data = (new UnicodeEncoding()).GetBytes(cmd); reader._pf.Transmit(data);

希望当你在找一个SSH库时这篇文章对你有所帮助,谢谢!

[温馨提示]:该文章由原博客园导入而来,如排版效果不佳,请移步:http://www.cnblogs.com/coderzh/archive/2008/05/20/1203574.html

微信扫一扫交流

作者:CoderZh
微信关注:hacker-thinking (一个程序员的思考)
本文出处:https://blog.coderzh.com/2008/05/20/1203574/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。