转载自:http://blog.csdn.net/lusonglin3g/article/details/4801247

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接口。首先引用命名空间:

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


Reader类实现如下:

None.gif class Reader : ISSHConnectionEventReceiver, ISSHChannelEventReceiver
ExpandedBlockStart.gif
{
InBlock.gif
public SSHConnection _conn;
InBlock.gif
publicbool _ready;
InBlock.gif
InBlock.gif
publicvoid OnData(byte[] data, int offset, int length)
ExpandedSubBlockStart.gif
{
InBlock.gif            System.Console.Write(Encoding.ASCII.GetString(data, offset, length));
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid OnDebugMessage(bool always_display, byte[] data)
ExpandedSubBlockStart.gif
{
InBlock.gif            Debug.WriteLine(
"DEBUG: "+ Encoding.ASCII.GetString(data));
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid OnIgnoreMessage(byte[] data)
ExpandedSubBlockStart.gif
{
InBlock.gif            Debug.WriteLine(
"Ignore: "+ Encoding.ASCII.GetString(data));
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid OnAuthenticationPrompt(string[] msg)
ExpandedSubBlockStart.gif
{
InBlock.gif            Debug.WriteLine(
"Auth Prompt "+ msg[0]);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
publicvoid OnError(Exception error, string msg)
ExpandedSubBlockStart.gif
{
InBlock.gif            Debug.WriteLine(
"ERROR: "+ msg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid OnChannelClosed()
ExpandedSubBlockStart.gif
{
InBlock.gif            Debug.WriteLine(
"Channel closed");
InBlock.gif            _conn.Disconnect(
"");
InBlock.gif
//_conn.AsyncReceive(this);
ExpandedSubBlockEnd.gif
       }

InBlock.gif
publicvoid OnChannelEOF()
ExpandedSubBlockStart.gif
{
InBlock.gif            _pf.Close();
InBlock.gif            Debug.WriteLine(
"Channel EOF");
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid OnExtendedData(int type, byte[] data)
ExpandedSubBlockStart.gif
{
InBlock.gif            Debug.WriteLine(
"EXTENDED DATA");
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid OnConnectionClosed()
ExpandedSubBlockStart.gif
{
InBlock.gif            Debug.WriteLine(
"Connection closed");
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid OnUnknownMessage(byte type, byte[] data)
ExpandedSubBlockStart.gif
{
InBlock.gif            Debug.WriteLine(
"Unknown Message "+ type);
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid OnChannelReady()
ExpandedSubBlockStart.gif
{
InBlock.gif            _ready
=true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid OnChannelError(Exception error, string msg)
ExpandedSubBlockStart.gif
{
InBlock.gif            Debug.WriteLine(
"Channel ERROR: "+ msg);
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid OnMiscPacket(byte type, byte[] data, int offset, int length)
ExpandedSubBlockStart.gif
{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
public PortForwardingCheckResult CheckPortForwardingRequest(string host, int port, string originator_host, int originator_port)
ExpandedSubBlockStart.gif
{
InBlock.gif            PortForwardingCheckResult r
=new PortForwardingCheckResult();
InBlock.gif            r.allowed
=true;
InBlock.gif            r.channel
=this;
InBlock.gif
return r;
ExpandedSubBlockEnd.gif        }

InBlock.gif
publicvoid EstablishPortforwarding(ISSHChannelEventReceiver rec, SSHChannel channel)
ExpandedSubBlockStart.gif
{
InBlock.gif            _pf
= channel;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
public SSHChannel _pf;
ExpandedBlockEnd.gif    }


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

None.gif class Program
ExpandedBlockStart.gif
{
InBlock.gif
privatestatic SSHConnection _conn;
InBlock.gif
staticvoid Main(string[] args)
ExpandedSubBlockStart.gif
{
InBlock.gif            SSHConnectionParameter f
=new SSHConnectionParameter();
InBlock.gif            f.UserName
="root";
InBlock.gif            f.Password
="****";
InBlock.gif            f.Protocol
= SSHProtocol.SSH2;
InBlock.gif            f.AuthenticationType
= AuthenticationType.Password;
InBlock.gif            f.WindowSize
=0x1000;
InBlock.gif            Reader reader
=new Reader();
InBlock.gif            Socket s
=new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
InBlock.gif            s.Connect(
new IPEndPoint(IPAddress.Parse("192.168.x.x"), 22));
InBlock.gif            _conn
= SSHConnection.Connect(f, reader, s);
InBlock.gif            reader._conn
= _conn;
InBlock.gif            SSHChannel ch
= _conn.OpenShell(reader);
InBlock.gif            reader._pf
= ch;
InBlock.gif            SSHConnectionInfo ci
= _conn.ConnectionInfo;
InBlock.gif
InBlock.gif            Thread.Sleep(
1000);
InBlock.gif
InBlock.gif
byte[] b =newbyte[1];
InBlock.gif
while (true)
ExpandedSubBlockStart.gif
{
InBlock.gif
int input = System.Console.Read();
InBlock.gif                b[
0] = (byte)input;
InBlock.gif                reader._pf.Transmit(b);
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }


4.执行效果如下:
cmd.JPG

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

None.gif byte [] b = new byte [ 1 ];
while ( true )
ExpandedBlockStart.gif
{
InBlock.gif
int input = System.Console.Read();
InBlock.gif      b[
0] = (byte)input;
InBlock.gif      reader._pf.Transmit(b);
ExpandedBlockEnd.gif }


替换为:

None.gif string cmd = " vi xxx.txt/n " ;
None.gif
byte [] data = ( new UnicodeEncoding()).GetBytes(cmd);
None.gifreader._pf.Transmit(data);