[C#]SharpSSH-一个可以使用SSH连接的.NET库

A Secure Shell (SSH) library for .NET

觉得有用,就记录下来了

http://www.tamirgal.com/blog/page/SharpSSH.aspx

http://sourceforge.net/projects/sharpssh/?source=typ_redirect

 

再来个例子供参考

 

public partial class SSHWnd : Form
{
    private SshShell ss = null;
    private Stream io = null; private SshConnectionInfo scInfo; private static byte[] buffer; private static int bufSize = 256; private static AsyncCallback readCallback; private delegate void addLineDelegate(string s); public SSHWnd(SshConnectionInfo scInfo) { InitializeComponent(); this.scInfo = scInfo; try { ss = new SshShell(scInfo.Host, scInfo.User); if (scInfo.Pass != null) { ss.Password = scInfo.Pass; } if (scInfo.IdentityFile != null) { ss.AddIdentityFile(scInfo.IdentityFile); } ss.Connect(22); io = ss.GetStream(); buffer = new byte[bufSize]; readCallback = new AsyncCallback(OnCompletedRead); io.BeginRead(buffer, 0, bufSize, readCallback, null); } catch { MessageBox.Show("Error!"); } } private void OnCompletedRead(IAsyncResult ar) { int bytesRead = io.EndRead(ar); if (bytesRead > 0) { String str = Encoding.UTF8.GetString(buffer, 0, bytesRead); this.Invoke(new addLineDelegate(addLine), new object[]{str}); io.BeginRead(buffer, 0, bufSize, readCallback, null); } } void addLine(string s) { textBox1.AppendText(s); } private void SSHWnd_FormClosing(object sender, FormClosingEventArgs e) { if (ss.Connected) { io.Close(); ss.Close(); } } private void button1_Click(object sender, EventArgs e) { if (textBox2.Text != "") { try { StreamWriter sw = new StreamWriter(io); sw.Write(textBox2.Text); sw.Write('\n'); sw.Flush(); } catch { MessageBox.Show("Terminated yet!"); this.Close(); } textBox2.Text = ""; } } }

转载于:https://www.cnblogs.com/boneking/p/4341650.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值