0
votes

I am writing a c# SSH tunneling program to consolidate dynamic tunneling into a single program, instead of using putty+firefox socks4/chrome socks4 plugin, typing in the same user/pass..ect. It is using the Renci.SSHNet library.

My question is this: When I create a new SshClient object, I want to be able to kill that connection later from a kill connection button, and also be able to run a separate thread that says while(isConnected) if (!addedToList) addtolist..The issue is that I am unsure of how to access different simultaneous running sshClient objects. Say I have 3 open connections to different hosts. If I select the host ip from current connections, I need a way to connect that ip address with the SshCLient connections and then run sshClient.Disconnect(). I realize that you can use sshClient.ConnectionInfo.Host.ToString() and match a string to that, but again if I am in a different method (kill connection button clicked) I don't know how to match host ip of 192.168.1.1 to an sshClient that is connected to that host.

I looked into weak references, but that seems to only work if I had only one sshClient. If there was a way to access currently created SshClient objects, then match it to the Host ip property, it would work. I can't find this in the Google.

I thought about scheming some elaborate way to create private object variable SshClients that are named after deviceIpString, but was unsure of how to create a variable that is named after another variable's contents (String). such as:

deviceIpString = 192.168.1.1;
(i am pretending that backticks access the variable's data, since I dont know how to do it)
private SshClient 'deviceIpString' = new (SshClient(arguments));
1
Welcome to Stack Overflow. The way that SO works is that answers should be posted as answers. Not as an edit to your question. Also, the way that we know that your question has been dealt with (to your satisfaction) is for you to accept an answer (not by editing your title and putting "resolved" or "answered" there). I'd recommend you visit the help centre or take the tour to get more familiar with SO.Damien_The_Unbeliever
Thanks for the help. I will adjust accordingly.Brandon

1 Answers

0
votes

Edit: I fixed the problem with a neat solution I have used in another program I wrote. I changed the name of question because this can be applied to any object of any type, as long as you can make an array of them. (not sure of a situation of where you can't, maybe a program only permits one object of that type)

The Solution: Create an private array to store all of the objects you want to reference later. Set it to a number of objects that you think is plausible to be created in the program. Then, create a private int variable for the index, starting at 0. Create a private void (or public if needed) method that accepts the object as an argument. The result should look like this:

private SshClient[] sshClients = new SshClient[10];
private int sshClientIndex = 0;
private void registerSshClient(SshClient sshClient)
{
  sshClients[sshClientIndex] = sshClient;
  ++sshClientIndex;
}

Then, when you get to the point where you are creating the object, add it into the array when necessary, using the register method. I do this after I verify the sshClient is connected. Then, come time when you need to do something with a specific object that you registered, find some type of data you can use to match it. My program adds the host ip to a listbox once the sshClient connects. I use this ip address to match the sshClient's connection info property. To match a certain object in the array, use a for loop. Here is my Kill Connection button for an example:

private void button_kill_connection_Click(object sender, EventArgs e)
    {
        Object selectedConnection = null;
        selectedConnection = lbox_current_connections.SelectedItem;
        String selectedConnectionString = selectedConnection.ToString();
        for (int i = 0; i <= --sshClientIndex; i++)
        {

            if (sshClients[i].ConnectionInfo.Host == selectedConnectionString)
            {
                sshClients[i].Disconnect();
                lbox_current_connections.Items.Remove(selectedConnection);
            }
        }
    }

The for loop starts at the first array element (0) and progresses up until it reaches the amount of connections (objects actually stored) registered.