Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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);
                }

            }
        }
    }


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

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900