zoukankan      html  css  js  c++  java
  • [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('
    ');
                    sw.Flush();
                }
                catch
                {
                    MessageBox.Show("Terminated yet!");
                    this.Close();
                }
    
                textBox2.Text = "";
            }
        }
    }
  • 相关阅读:
    赢在中国让创业不再孤独
    职场心得:关于资源整合!
    中国遭遇第三次单身危机
    成功人士个人财富增长的15种能力
    工作三年之后的十三种痛!
    创业需要什么——第一篇 思维能力和行动能力YC
    忠言多少有些逆耳,创业的九条真经
    培养你的品格
    任何一个创业者都要注意的22个经典提示
    获益匪浅:在北京每月能白捡一万元
  • 原文地址:https://www.cnblogs.com/boneking/p/4341650.html
Copyright © 2011-2022 走看看