码迷,mamicode.com
首页 > 其他好文 > 详细

SharpSSH Demo

时间:2019-11-25 23:56:39      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:ddc   error   click   abs   you   gate   stream   password   target   

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 = "";
        }
    }
}

  没有英汉互译结果
  请尝试网页搜索

SharpSSH Demo

标签:ddc   error   click   abs   you   gate   stream   password   target   

原文地址:https://www.cnblogs.com/qjf1111/p/11931266.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!