I need to establish a SSH tunnel from Sql Server 2005 to a remote SQL
Server.
Then sync a local table with the remote table.
I need to set it up as a scheduled job.
I have created a batch file that runs PLINK to do the port forwarding.
I am executing this batch file via xp_cmdshell on my local sql server
or I can just use xp_cmdsheel to execute the Plink command.
My command
plink -batch -l administrator -pw 339847 -L 1434:localhost:1433
192.168.33.97
When Plink is running I can connect to the remote server and run my
SQL code.
This all works when I run plink from one query window and the stored
procedure from another.
But when I try to combine plink and execute the sync stored procedure
into one script,
as far as SQL is concerned the plink code never finishes
So it never executes the stored procedure.
My second problem is I can't close Plink gracefully.
I can use TASKKILL /F /IM "plink.exe" to Kill it but that is not
ideal.
Is there way I can script Plink to run first, then execute a sql
stored procedure and exit?
see current script below
Thank you for your help!
----------------------------------------------------------------------------------------------------------------------
--
-----------------------------------------------------------------------------------------------------------------------
DECLARE @cmd varchar(1000)
----------------------------------------------------------------------------------------------------------------------
--Create SSH Tunnel
----------------------------------------------------------------------------------------------------------------------
SELECT @cmd = 'plink -batch -l administrator -pw 339847 -L
1434:localhost:1433 192.168.33.97'
---- Drop table #Results if it exists (swallow the error if not)
--BEGIN TRY DROP TABLE [#Results]; END TRY BEGIN CATCH END CATCH;
--CREATE TABLE #Results (id int identity(1,1), s varchar(1000))
--
--INSERT #Results
EXEC master..xp_cmdshell @cmd ,no_output
--SELECT id, ouputtmp = s from #Results
----------------------------------------------------------------------------------------------------------------------
--Sync Tables
----------------------------------------------------------------------------------------------------------------------
DECLARE @RC int
EXECUTE @RC = [Database].[dbo].[stp_Sync_Data]
----------------------------------------------------------------------------------------------------------------------
--Kill SSH Tunnel
----------------------------------------------------------------------------------------------------------------------
SELECT @cmd = 'TASKKILL /F /IM "plink.exe"'
EXEC master..xp_cmdshell @cmd