su Supported by Chilkat SSH for Linux/Unix Servers?


I don’t see anything in the documentation or examples to indicate that “su” is supported w/ SSH?  Is it possible to login to a user account and then “su” to root?


“su” is a command just like any other Unix/Linux command. It is typed at the command prompt, it does something, and a response is written to standard output (possibly nothing more than new command prompt). Therefore, any example you see for starting a remote shell and running commands would apply with “su”.

To put it simply: Yes, “su” is supported because it’s a command just like “ls”, “cat”, etc.

SFTP and SSH: Separate Connections Required?


I have an application using your code that does several SSH and SFTP command during processing. Can I just establish a connection, authenticate passwords and the other setup steps once and then use that connection throughout the program or do I need to perform these steps in every function? If I can do I need a separate connection for SFTP and for SSH?


You’ll need one connection for SFTP, and one connection for SSH.  So  you need to have one instance of an SFTP object and one instance of the SSH object.   Therefore, you have two connections (one in each object).

You should be able to maintain those connections.  With SSH, you may open and close logical channels.  In fact, you may have any number of logical channels open simultaneously on the same connection.   With SFTP, you may upload and download any number of files on the same connection.

One thing to beware of, and this usually applies to any client-server protocol, is that if the client becomes inactive for some period of time the server may decide to close the connection.  This time limit is entirely up to the server and the client has no control over it.  Therefore, you might have  your application periodically send a “no-op” message to the server when it is inactive.  With SSH, you would do it by calling SendIgnore.  With SFTP you might call RealPath(“.”,””)  just for the sake of generating traffic.

However, even with the “no-op” strategy, your application should still be able to handle the chance that the server may disconnect, or you may lose the connection for some external reason, at any time.

SSH: Failed to read 1st key exchange packet

Here’s an explanation for the following error message:

    Established TCP/IP connection with SSH server
    FromServer: SSH-1.5-Cisco-1.25
    numBytesRequested: 8
    Connection closed by server.
    Failed to read data on SSH connection.
    Failed to read 1st key exchange packet

The error message indicates that as soon as the TCP/IP socket connection was accepted
by the SSH server, it then decided to disconnect. No data was exchanged over the socket connection. In other words, you didn’t even receive the initial “hello” message.

The SSH server probably rejected the connection based on the IP address from which you’re connecting.

SSH Tunneling (Port Forwarding)

SSH Port Forwarding (or tunneling) allows you to tunnel any TCP connection through an SSH server. For example, consider a database connection:

A direct TCP connection:

DbClient <----TCP---->  DbServer

An SSH tunneled connection:

DbClient <----TCP----> SshClient <====SSH====> SshServer <----TCP----> DbServer

In a tunneled connection, the application connects through an SshClient to an SSH server and starts a direct-tcpip channel, specifying the destination host:port (i.e. the database server). Historically, the SshClient has been a standalone program, such as PuTTY, that typically runs on the same computer as the DbClient. We’ll show you later in this article how the SshClient is merged directly into your application to eliminate the need for a standalone SSH go-between to be running wherever your application runs. This is the power of Chilkat SSH tunneling: your application can create tunnels without requiring external software such as PuTTY to be installed and running.

The SSH Server may run on the same computer as the DbServer, or anywhere else. The typical situation is that both the SSH server and database server are within the same firewall. The firewall typically allows traffic to pass through port 22 to the SSH server, but not to the database server. Communications between the SSH server and database server are not secure, but since they occur behind a firewall, it’s not a problem.

A tunnel can be established to anything, not just a database server. For example:

HttpClient <----TCP----> SshClient <====SSH====> SshServer <----TCP----> HTTP Web Server
SmtpClient <----TCP----> SshClient <====SSH====> SshServer <----TCP----> SMTP Email Server
Pop3Client <----TCP----> SshClient <====SSH====> SshServer <----TCP----> POP3 Email Server
ImapClient <----TCP----> SshClient <====SSH====> SshServer <----TCP----> IMAP Email Server
TcpClient <----TCP----> SshClient <====SSH====> SshServer <----TCP----> Custom TCP Socket Application

Prior to Chilkat, SSH tunneling required a separate client-side program (or Windows Service) to serve as the SSH2 port forwarding client. (This is the SshClient in the diagram above.) This is an added piece of infrastructure that must be installed and running in order for your application to use SSH tunneling. This adds complexity to your application’s deployment, is a potential source of failure, and represents a hidden cost of ongoing support for your application. (Chilkat always recommends minimizing infrastructure and complexity.)

Chilkat provides three solutions to merge the SshClient directly into your application:

  1. Integration with the protocol API. Chilkat’s API’s for SMTP, POP3, and IMAP have been extended with SSH tunneling methods. Using an SSH tunnel with these API’s is simple: Establish the SSH tunnel by calling SshTunnel(hostname,port), then authenticate by calling SshAuthenticatePw(login,password). This creates the tunnel, and the remainder of the IMAP, POP3, or SMTP programming is identical to the non SSH-tunnel case. (See the following examples: Integrated POP3 SSH TunnelingIntegrated SMTP SSH Tunneling, Integrated IMAP SSH Tunneling.
  2. Use the Chilkat SshTunnel class/object to create the “SshClient” in a background thread of the application. This is a good solution when using non-Chilkat API’s that require a hostname:port for a connection, such as with database programming (ADO, ODBC, OLE DB, etc.) Your application would instantiate an SshTunnel object, set various properties (SSH server hostname/port, database server hostname/port, SSH login, etc.) then then start the background thread by calling SshTunnel.BeginAccepting. The SshTunnel runs autonomously in a background thread, accepting connections and managing bi-directional SSH tunnels. Here are examples:  Background Thread SSH Tunneling
  3. Use Chilkat SSH to create a direct-tcpip channel via the Ssh.OpenDirectTcpIpChannel method. Your application may then send and receive data through the SSH tunnel by calling various Chilkat SSH send/receive methods. This solution is good for when the destination server is a custom TCP socket server (i.e. it uses a custom application-specific protocol that you’ve designed).  Here are examples: direct-tcpip Port Forwarding

SSH direct-tcpip Port Forwarding (tunneling)

ASP: SSH Tunnel (Port Forwarding via direct-tcpip channel)
SQL Server: SSH Tunnel (Port Forwarding via direct-tcpip channel)
C#: SSH Tunnel (Port Forwarding via direct-tcpip channel)
C++: SSH Tunnel (Port Forwarding via direct-tcpip channel)
MFC: SSH Tunnel (Port Forwarding via direct-tcpip channel)
C: SSH Tunnel (Port Forwarding via direct-tcpip channel)
Delphi: SSH Tunnel (Port Forwarding via direct-tcpip channel)
Visual FoxPro: SSH Tunnel (Port Forwarding via direct-tcpip channel)
Java: SSH Tunnel (Port Forwarding via direct-tcpip channel)
Perl: SSH Tunnel (Port Forwarding via direct-tcpip channel)
PHP: SSH Tunnel (Port Forwarding via direct-tcpip channel)
Python: SSH Tunnel (Port Forwarding via direct-tcpip channel)
Ruby: SSH Tunnel (Port Forwarding via direct-tcpip channel)
VB.NET: SSH Tunnel (Port Forwarding via direct-tcpip channel)
Visual Basic: SSH Tunnel (Port Forwarding via direct-tcpip channel)
VBScript: SSH Tunnel (Port Forwarding via direct-tcpip channel)

SSH – Multiple Commands w/out Reconnect


Hi, I’m evaluating Chilkat C++ SSH Library for My VC 9.0 Projects.
I have a question: Your examples send only one command.
I want to send several commands without reconnect.
I experimented and found SendReqShell handles multiple commands execution, not SendReqExec.
Can I have a more detailed sample about this?

You may call SendReqExec multiple times on the same open channel.

Bitvise WinSSHD request “dumb” PTY problem

I’ve found the following scenario when testing SSH shell with the Bitvise WinSSHD server. If a request for a “dumb” pseudo-terminal is sent via the Chilkat SSH component’s SendReqPty method, the WinSSHD server responds with a success status, however, it really fails. The SSHD log has the following error message:

000000000116 2008-11-19 16:29:41.185Z WinSSHD 5.01 [297] Warning
  Session thread 1014 for Windows account 'DOTNET\test123' from
  Session channel 1: Error initializing terminal 'dumb': Loading terminfo file failed, call stack:

  - CreateFile() failed: Windows error 2: The system cannot find the file specified.
  - TermInfo::Load(): failed at opening file

000000000117 2008-11-19 16:29:41.388Z WinSSHD 5.01 [050] Info
  Session thread 1014 for Windows account 'DOTNET\test123' from
  Session channel 1: Closing session channel. 

The session channel is closed and all subsequent attempts at communication on the channel will fail.

POP3 SSH Tunneling (Port Forwarding)

ASP: POP3 SSH Tunneling (Port Forwarding)
SQL Server: POP3 SSH Tunneling (Port Forwarding)
C#: POP3 SSH Tunneling (Port Forwarding)
C++: POP3 SSH Tunneling (Port Forwarding)
MFC: POP3 SSH Tunneling (Port Forwarding)
C: POP3 SSH Tunneling (Port Forwarding)
Delphi: POP3 SSH Tunneling (Port Forwarding)
Visual FoxPro: POP3 SSH Tunneling (Port Forwarding)
Java: POP3 SSH Tunneling (Port Forwarding)
Perl: POP3 SSH Tunneling (Port Forwarding)
PHP: POP3 SSH Tunneling (Port Forwarding)
Python: POP3 SSH Tunneling (Port Forwarding)
Ruby: POP3 SSH Tunneling (Port Forwarding)
VB.NET: POP3 SSH Tunneling (Port Forwarding)
Visual Basic: POP3 SSH Tunneling (Port Forwarding)
VBScript: POP3 SSH Tunneling (Port Forwarding)

SMTP SSH Tunneling (Port Forwarding)

ASP: SMTP SSH Tunneling (Port Forwarding)
SQL Server: SMTP SSH Tunneling (Port Forwarding)
C#: SMTP SSH Tunneling (Port Forwarding)
C++: SMTP SSH Tunneling (Port Forwarding)
MFC: SMTP SSH Tunneling (Port Forwarding)
C: SMTP SSH Tunneling (Port Forwarding)
Delphi: SMTP SSH Tunneling (Port Forwarding)
Visual FoxPro: SMTP SSH Tunneling (Port Forwarding)
Java: SMTP SSH Tunneling (Port Forwarding)
Perl: SMTP SSH Tunneling (Port Forwarding)
PHP: SMTP SSH Tunneling (Port Forwarding)
Python: SMTP SSH Tunneling (Port Forwarding)
Ruby: SMTP SSH Tunneling (Port Forwarding)
VB.NET: SMTP SSH Tunneling (Port Forwarding)
Visual Basic: SMTP SSH Tunneling (Port Forwarding)
VBScript: SMTP SSH Tunneling (Port Forwarding)