Well SQL Server can't do this natively, so you're going to have to go outside of your comfort zone a little in any event. Also by "on the server" do you mean on the same physical server where SQL Server is running, or do you mean "inside the SQL Server instance"?
You could certainly call a local PowerShell script from xp_cmdshell
inside a stored procedure, or as part of a SQL Server Agent job step if you need it to be asynchronous (and if you're not running Express Edition, which doesn't have Agent). Here is a PowerShell script that wouldn't take ultimate PowerShell mastery to adapt to your needs:
Automating Telnet with PowerShell
In order to call this from within a stored procedure, you'll first need to be sure that xp_cmdshell
is enabled:
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE WITH OVERRIDE;
GO
EXEC sp_configure 'xp_cmdshell', 1;
EXEC sp_configure 'show advanced options', 0;
RECONFIGURE WITH OVERRIDE;
GO
As a caution, a lot of people view exposing xp_cmdshell
as a bird-flu-like pandemic. To me it's all about controlling access to the machine not about controlling what the machine can and can't do to fulfill your business requirements.
Here is a quick sample of consuming the output of whatever you send to xp_cmdhsell
. This is just a standard command-line call, so you'll have to adapt it for calling PowerShell scripts, but it should work largely the same:
CREATE TABLE #tmp(i INT IDENTITY(1,1), x VARCHAR(2048));
INSERT #tmp(x) EXEC master..xp_cmdshell 'dir C:\Users\';
SELECT x FROM #tmp ORDER BY i;
DROP TABLE #tmp;
Results:
Volume in drive C has no label.
Volume Serial Number is 50D3-008B
NULL
Directory of C:\Users
NULL
01/10/2012 09:20 AM <DIR> .
01/10/2012 09:20 AM <DIR> ..
01/19/2011 11:54 PM 1,444 SomeFile.txt
.... other files...
3 File(s) 28,918,500 bytes
18 Dir(s) 19,367,292,416 bytes free
NULL
But once you've spent more than an hour messing with this to get it to work, I think you're really better off writing an application that either runs as a service or on an interval, checks a queue table for new telnet processing to, does the work, then writes the results to SQL Server. Your RDBMS shouldn't be treated like a batch scripting host IMHO.