Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Total newbie help needed. 2

Status
Not open for further replies.

eraH

MIS
Joined
Dec 1, 2003
Messages
106
Location
NZ
Ok, I've never programmed before so please be gentle and type very slowly when replying :)

What I'm trying to do is connect to a Linux box and issue commands.

I've managed to connect to the Linux server when the "Connect" button is clicked. But how do I
1. Keep the connection open? If I click on the next tab in the form the connection closes.
2. I have to create the following variable every time.
Code:
SshExec exec = new SshExec(ip_textbox.Text, username_textbox.Text, password_textbox.Text);

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tamir.SharpSsh; 

namespace LMC
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        
        private void connect_button_Click(object sender, EventArgs e)
        {
            //Connects to server using details entered by user under logon tab

            SshExec exec = new SshExec(ip_textbox.Text, username_textbox.Text, password_textbox.Text);
            exec.Connect();
            
            //If the connection is successful, it changes the connection status label
            connectionstatus_label.ForeColor = Color.Green;
            connectionstatus_label.Text = "Status: Connected";
        }

        private void ServerInfo_Tab_Click(object sender, EventArgs e)
        {
            //Runs a command on the server, just for testing purposes
            string command;
            command = "cat /etc/redhat-release";
            string output = exec.RunCommand(command);
            osrelease_textbox.Text = output;
        }
   
    }
}

Thank you in advance for any replies.
 
Code:
SshExec exec = new SshExec(ip_textbox.Text, username_textbox.Text, password_textbox.Text);
            exec.Connect();
The exec variable is local to your click event handler. When the handler ends, it goes away.

Define it at a higher scope level -- like in the class itself.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
How would I do that, the
Code:
exec.Connect();
part starts the actual connection. How would the program know when the user has entered the connection details?

I think I'm in over my head, I might just do a bit more research and try and figure this out.

Thanks for your help.
 
What chip is saying is:

Define your SshExec in the class and then use it inside your button click. Anything you define within a method (in your case connect_button_Click(obejct sender, EventArgs e) will only be available until the code inside that method has completed.

so

Code:
public partial class MainForm : Form
    {
        SshExec exec  = null;

        public MainForm()
        {
            InitializeComponent();
        }
        
        private void connect_button_Click(object sender, EventArgs e)
        {
            //Connects to server using details entered by user under logon tab

            exec = new SshExec(ip_textbox.Text, username_textbox.Text, password_textbox.Text);
            exec.Connect();
            
            //If the connection is successful, it changes the connection status label
            connectionstatus_label.ForeColor = Color.Green;
            connectionstatus_label.Text = "Status: Connected";
        }

        private void ServerInfo_Tab_Click(object sender, EventArgs e)
        {
            //Now you can run your commands on the exec object

            exec.SendCommand("or whatever the call is");



            //Runs a command on the server, just for testing purposes
            string command;
            command = "cat /etc/redhat-release";
            string output = exec.RunCommand(command);
            osrelease_textbox.Text = output;
        }
   
    }


I suggest attaching to the FormClosing event and calling exec.Close(); to ensure the connection is closed properly when you quit the application.

 
Cool, thanks a lot to both of you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top