轉載自: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接口。首先引用命名空間:
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;
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
{
public SSHConnection _conn;
publicbool _ready;
publicvoid OnData(byte[] data, int offset, int length)
{
System.Console.Write(Encoding.ASCII.GetString(data, offset, length));
}
publicvoid OnDebugMessage(bool always_display, byte[] data)
{
Debug.WriteLine("DEBUG: "+ Encoding.ASCII.GetString(data));
}
publicvoid OnIgnoreMessage(byte[] data)
{
Debug.WriteLine("Ignore: "+ Encoding.ASCII.GetString(data));
}
publicvoid OnAuthenticationPrompt(string[] msg)
{
Debug.WriteLine("Auth Prompt "+ msg[0]);
}
publicvoid OnError(Exception error, string msg)
{
Debug.WriteLine("ERROR: "+ msg);
}
publicvoid OnChannelClosed()
{
Debug.WriteLine("Channel closed");
_conn.Disconnect("");
//_conn.AsyncReceive(this);
}
publicvoid OnChannelEOF()
{
_pf.Close();
Debug.WriteLine("Channel EOF");
}
publicvoid OnExtendedData(int type, byte[] data)
{
Debug.WriteLine("EXTENDED DATA");
}
publicvoid OnConnectionClosed()
{
Debug.WriteLine("Connection closed");
}
publicvoid OnUnknownMessage(byte type, byte[] data)
{
Debug.WriteLine("Unknown Message "+ type);
}
publicvoid OnChannelReady()
{
_ready =true;
}
publicvoid OnChannelError(Exception error, string msg)
{
Debug.WriteLine("Channel ERROR: "+ msg);
}
publicvoid OnMiscPacket(byte type, byte[] data, int offset, int length)
{
}
public PortForwardingCheckResult CheckPortForwardingRequest(string host, int port, string originator_host, int originator_port)
{
PortForwardingCheckResult r =new PortForwardingCheckResult();
r.allowed =true;
r.channel =this;
return r;
}
publicvoid EstablishPortforwarding(ISSHChannelEventReceiver rec, SSHChannel channel)
{
_pf = channel;
}
public SSHChannel _pf;
}
{
public SSHConnection _conn;
publicbool _ready;
publicvoid OnData(byte[] data, int offset, int length)
{
System.Console.Write(Encoding.ASCII.GetString(data, offset, length));
}
publicvoid OnDebugMessage(bool always_display, byte[] data)
{
Debug.WriteLine("DEBUG: "+ Encoding.ASCII.GetString(data));
}
publicvoid OnIgnoreMessage(byte[] data)
{
Debug.WriteLine("Ignore: "+ Encoding.ASCII.GetString(data));
}
publicvoid OnAuthenticationPrompt(string[] msg)
{
Debug.WriteLine("Auth Prompt "+ msg[0]);
}
publicvoid OnError(Exception error, string msg)
{
Debug.WriteLine("ERROR: "+ msg);
}
publicvoid OnChannelClosed()
{
Debug.WriteLine("Channel closed");
_conn.Disconnect("");
//_conn.AsyncReceive(this);
}
publicvoid OnChannelEOF()
{
_pf.Close();
Debug.WriteLine("Channel EOF");
}
publicvoid OnExtendedData(int type, byte[] data)
{
Debug.WriteLine("EXTENDED DATA");
}
publicvoid OnConnectionClosed()
{
Debug.WriteLine("Connection closed");
}
publicvoid OnUnknownMessage(byte type, byte[] data)
{
Debug.WriteLine("Unknown Message "+ type);
}
publicvoid OnChannelReady()
{
_ready =true;
}
publicvoid OnChannelError(Exception error, string msg)
{
Debug.WriteLine("Channel ERROR: "+ msg);
}
publicvoid OnMiscPacket(byte type, byte[] data, int offset, int length)
{
}
public PortForwardingCheckResult CheckPortForwardingRequest(string host, int port, string originator_host, int originator_port)
{
PortForwardingCheckResult r =new PortForwardingCheckResult();
r.allowed =true;
r.channel =this;
return r;
}
publicvoid EstablishPortforwarding(ISSHChannelEventReceiver rec, SSHChannel channel)
{
_pf = channel;
}
public SSHChannel _pf;
}
3.好的,現在來測試一下:
class Program
{
privatestatic SSHConnection _conn;
staticvoid Main(string[] args)
{
SSHConnectionParameter f =new SSHConnectionParameter();
f.UserName ="root";
f.Password ="****";
f.Protocol = SSHProtocol.SSH2;
f.AuthenticationType = AuthenticationType.Password;
f.WindowSize =0x1000;
Reader reader =new Reader();
Socket s =new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(new IPEndPoint(IPAddress.Parse("192.168.x.x"), 22));
_conn = SSHConnection.Connect(f, reader, s);
reader._conn = _conn;
SSHChannel ch = _conn.OpenShell(reader);
reader._pf = ch;
SSHConnectionInfo ci = _conn.ConnectionInfo;
Thread.Sleep(1000);
byte[] b =newbyte[1];
while (true)
{
int input = System.Console.Read();
b[0] = (byte)input;
reader._pf.Transmit(b);
}
}
}
{
privatestatic SSHConnection _conn;
staticvoid Main(string[] args)
{
SSHConnectionParameter f =new SSHConnectionParameter();
f.UserName ="root";
f.Password ="****";
f.Protocol = SSHProtocol.SSH2;
f.AuthenticationType = AuthenticationType.Password;
f.WindowSize =0x1000;
Reader reader =new Reader();
Socket s =new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(new IPEndPoint(IPAddress.Parse("192.168.x.x"), 22));
_conn = SSHConnection.Connect(f, reader, s);
reader._conn = _conn;
SSHChannel ch = _conn.OpenShell(reader);
reader._pf = ch;
SSHConnectionInfo ci = _conn.ConnectionInfo;
Thread.Sleep(1000);
byte[] b =newbyte[1];
while (true)
{
int input = System.Console.Read();
b[0] = (byte)input;
reader._pf.Transmit(b);
}
}
}
4.執行效果如下:
5.如果你需要快速的執行某些指定的命令,則可以把上面的
byte[] b =newbyte[1];
while (true)
{
int input = System.Console.Read();
b[0] = (byte)input;
reader._pf.Transmit(b);
}
while (true)
{
int input = System.Console.Read();
b[0] = (byte)input;
reader._pf.Transmit(b);
}
替換爲:
string cmd ="vi xxx.txt/n";
byte[] data = (new UnicodeEncoding()).GetBytes(cmd);
reader._pf.Transmit(data);
byte[] data = (new UnicodeEncoding()).GetBytes(cmd);
reader._pf.Transmit(data);