ComponentPro UltimateTerminal

      Reusing SSH session

      Language Filter: AllSend comments on this topic to ComponentPro

      It is possible to use the SshClient, SshTerminalControl and Sftp classes together to execute commands and transfer files in one session. The following example shows you how to reuse the connection of an SshClient instance to list names of files and directories on an SFTP server (the SshClient and SshTerminalControl classes also support that "reuse connection" feature):

      using System;
      using ComponentPro.Net;
      using ComponentPro.Net.Terminal;
      
      ...
      
      // Create an SshClient object.
      SshClient client = new SshClient();
      
      // Connect to the specified server.
      client.Connect("server");
      
      // Authenticate.
      client.Authenticate("user", "password");
      
      client.Timeout = 25000;
      
      TerminalShell sshShell = client.CreateShell(true);
      
      // Send the command to the SSH server and read response. 
      string response = sshShell.ExecuteCommand("ls");
      
      // Print out the response.
      Console.Write(response);
      
      // Initialize a new Sftp instance.
      Sftp sftp = new Sftp();
      
      // Reuse the connection.
      sftp.ReuseConnection(client);
      
      // Display a file list.
      Console.WriteLine(string.Join("\n", sftp.ListName()));
      
      // Close the connection.
      client.Disconnect();