使用Tamir.SharpSsh的SSH [英] SSH using Tamir.SharpSsh

查看:705
本文介绍了使用Tamir.SharpSsh的SSH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地主机上运行了一个自定义ssh服务器.我在尝试从另一种方法访问ssh.write时遇到麻烦.这是我所拥有的:

I have a custom ssh server running on my localhost. I am having trouble trying to access ssh.write from another method. here is what I have:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tamir.Streams;
using Tamir.SharpSsh;

namespace rapidShell
{
    public partial class Form1 : Form
    {
        string host, username, password;
        string nl = Environment.NewLine;

        public Form1()
        {            
            InitializeComponent();            
        }

        private void tbpassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SshConnect();
            }
        }

        private void tbhost_TextChanged(object sender, EventArgs e)
        {
            host = tbhost.Text;
        }

        private void tbusername_TextChanged(object sender, EventArgs e)
        {
            username = tbusername.Text;
        }

        private void tbpassword_TextChanged(object sender, EventArgs e)
        {
            password = tbpassword.Text;
        }

        private void textBox9_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                SshConnect();
            }
        }

        public void SshConnect()
        {    
            try
            {  
                tbStream.Text = "  -Connecting.." + nl + nl;
                SshStream ssh = new SshStream(host, username, password); // <-----I need to access this 'ssh.write' from a seperate method to run commands
                    
                    tbStream.Text += "\tOk (" + ssh.Cipher + "/" + ssh.Mac + ")" + nl;
                    tbStream.Text += "\tServer version=" + ssh.ServerVersion + nl;
                    tbStream.Text += "\tClient version=" + ssh.ClientVersion + nl + nl;
                    tbStream.Text += "\tUse the 'exit' command to disconnect" + nl + nl;
                    ssh.Prompt = ">";
                    ssh.RemoveTerminalEmulationCharacters = false;
                    tbStream.Text += ssh.ReadResponse();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "RapidSsh - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }
    }



如何通过程序全局访问"ssh.write()"?我还留下了评论以指出我的负担.



How can I globally access ''ssh.write()'' through my program? I also left a comment to point out my burden.

推荐答案

不要在快速解答"下发布该文章-如果您从某篇文章中获得了代码,那么这里有该文章底部的新消息"按钮,该按钮会将电子邮件发送给作者.然后会通知他们您希望与他们交谈.
将其发布到此处依赖于他们的"drop by"并意识到它是针对他们的.

但是就您提供的代码而言,您不能.
您不能在定义它的try...catch块之外访问ssh,更不用说SshStream可能包含的任何方法了.
Don''t post this under Quick Answers - if you got the code from an article, then there is a "new message" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.

But in the case of the code you presented, you can''t.
You cannot access ssh outside the try...catch block in which you define it, much less any methods which an SshStream may contain.


这篇关于使用Tamir.SharpSsh的SSH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆