热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Granados是一个基于.NET的SSH客户端库。它有以下特点:1.Granados是一个C#的开源项目。源码地址:点击进入2.同时支持SSH1和SSH2。3.Granados实现了AES,Bl
 Granados是一个基于.NET的SSH客户端库。它有以下特点:
1.Granados是一个C#的开源项目。源码地址: 点击进入
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
{
public SSHConnection _conn;
public bool _ready;

public void OnData(byte[] data, int offset, int length)
{
string sss = Encoding.ASCII.GetString(data, offset, length);
MessageBox.Show(sss);
}
public void OnDebugMessage(bool always_display, byte[] data)
{
Debug.WriteLine("DEBUG: " + Encoding.ASCII.GetString(data));
}
public void OnIgnoreMessage(byte[] data)
{
Debug.WriteLine("Ignore: " + Encoding.ASCII.GetString(data));
}
public void OnAuthenticationPrompt(string[] msg)
{
Debug.WriteLine("Auth Prompt " + msg[0]);
}

public void OnError(Exception error, string msg)
{
Debug.WriteLine("ERROR: " + msg);
}
public void OnChannelClosed()
{
Debug.WriteLine("Channel closed");
_conn.Disconnect("");
//_conn.AsyncReceive(this);
}
public void OnChannelEOF()
{
_pf.Close();
Debug.WriteLine("Channel EOF");
}
public void OnExtendedData(int type, byte[] data)
{
Debug.WriteLine("EXTENDED DATA");
}
public void OnConnectionClosed()
{
Debug.WriteLine("Connection closed");
}
public void OnUnknownMessage(byte type, byte[] data)
{
Debug.WriteLine("Unknown Message " + type);
}
public void OnChannelReady()
{
_ready = true;
}
public void OnChannelError(Exception error, string msg)
{
Debug.WriteLine("Channel ERROR: " + msg);
}
public void 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;
}
public void EstablishPortforwarding(ISSHChannelEventReceiver rec, SSHChannel channel)
{
_pf = channel;
}

public SSHChannel _pf;
}

3.好的,现在来测试一下:
class Program
{
private static SSHConnection _conn;
static void 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 = new byte[1];
while (true)
{
int input = System.Console.Read();
b[0] = (byte)input;
reader._pf.Transmit(b);
}

}
}


4.执行效果如下:

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

byte[] b = new byte[1];
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);

6.执行一般命令没问题,执行top命令会返回很多无法识别的字符,不知道是什么原因造成的。刚开始想写在web里的,但是,不等ondata()数据接收完毕连接就会断开,就费了点事情在winform中实现,

   public string ssh_conn(string ip, string root, string pass, string command)
{

SshStream ssh = new SshStream(ip, root, pass);
ssh.Prompt = "#";
ssh.RemoveTerminalEmulatiOnCharacters= true;
//Writing to the SSH channel
string respOnse= ssh.ReadResponse();
ssh.Write(command);
ssh.Flush();
ssh.Write("/n");
//Reading from the SSH channel
respOnse= ssh.ReadResponse();
MessageBox.Show(response);
return response;

}


 


推荐阅读
author-avatar
高山流水2602934240
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有