Chilkat
HOME
Android™
ASP
Visual Basic
VB.NET
C#
iOS (IPhone)
Objective-C
C++
C
MFC
Delphi
FoxPro
Java
Perl
PHP Extension
PHP ActiveX
Python
PowerShell
Ruby
SQL Server
VBScript
|
SSH Tunnel for Database Connection (such as ADO, ODBC, etc.)Demonstrates how to create an SSH tunneling client in a background thread of your application. This makes it possible to SSH tunnel database connections without the need for separate software (such as PuTTY) to be running. CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @sTmp0 nvarchar(4000) DECLARE @sshTunnel int EXEC @hr = sp_OACreate 'Chilkat.SshTunnel', @sshTunnel OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @sshTunnel, 'UnlockComponent', @success OUT, '30-day trial' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @sshTunnel, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- The destination host/port is the database server. -- The DestHostname may be the domain name or -- IP address (in dotted decimal notation) of the database -- server. EXEC sp_OASetProperty @sshTunnel, 'DestPort', 1433 EXEC sp_OASetProperty @sshTunnel, 'DestHostname', 'myDbServer.com' -- Provide information about the location of the SSH server, -- and the authentication to be used with it. This is the -- login information for the SSH server (not the database server). EXEC sp_OASetProperty @sshTunnel, 'SshHostname', '192.168.1.108' EXEC sp_OASetProperty @sshTunnel, 'SshPort', 22 EXEC sp_OASetProperty @sshTunnel, 'SshLogin', 'mySshLogin' EXEC sp_OASetProperty @sshTunnel, 'SshPassword', 'mySshPassword' -- Start accepting connections in a background thread. -- The SSH tunnels are autonomously run in a background -- thread. There is one background thread for accepting -- connections, and another for managing the tunnel pool. DECLARE @listenPort int SELECT @listenPort = 3316 EXEC sp_OAMethod @sshTunnel, 'BeginAccepting', @success OUT, @listenPort IF @success <> 1 BEGIN EXEC sp_OAGetProperty @sshTunnel, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- At this point you may connect to the database server through -- the SSH tunnel. Your database connection string would -- use "localhost" for the hostname and 3316 for the port. -- We're not going to show the database coding here, -- because it can vary depending on the API you're using -- (ADO, ODBC, OLE DB, etc. ) -- This is where your database code would go... -- When you're finished with the database connection, you may -- stop the background tunnel threads: -- Stop the background thread that accepts new connections: EXEC sp_OAMethod @sshTunnel, 'StopAccepting', @success OUT IF @success <> 1 BEGIN EXEC sp_OAGetProperty @sshTunnel, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END -- If any background tunnels are still in existence (and managed -- by a single SSH tunnel pool background thread), stop them... DECLARE @maxWaitMs int SELECT @maxWaitMs = 1000 EXEC sp_OAMethod @sshTunnel, 'StopAllTunnels', @success OUT, @maxWaitMs IF @success <> 1 BEGIN EXEC sp_OAGetProperty @sshTunnel, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 RETURN END END GO |
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.