winxp上传文件到服务器,通过SQLServer的xp_cmdshell在服务器之间传送文件
xp_cmdshell作為sql Server的擴(kuò)展存儲(chǔ)過(guò)程之一,也是sql Server在安全大敵,很多sql安全手冊(cè)上都要求關(guān)閉此過(guò)程,這不利用其特性簡(jiǎn)要實(shí)現(xiàn)了一個(gè)在sql服務(wù)器之間傳取文件的功能,在sql2005下測(cè)試通過(guò),現(xiàn)貼出代碼下,大家共賞之
/*
* 腳本:通過(guò)sqlServer的xp_cmdshell在服務(wù)器之間傳送文件
通過(guò)sql sqlServer實(shí)現(xiàn),要求支持遠(yuǎn)程訪問(wèn),并且均開(kāi)啟xp_cmdshell選項(xiàng)
* 作者: qin
* 整理: 2013.07.07 13:20
*
* E-Mail:QinGang@sina.com
* QQ:45958462
*/
/*
--開(kāi)啟高級(jí)選項(xiàng)
EXEC sp_configure 'show Advanced options','1'
RECONFIGURE
EXEC sp_configure 'xp_cmdshell',1
RECONFIGURE WITH OVERRIDE
*/
set nocount on;
--參數(shù)
declare @FromInstrance nvarchar(100),@FromDB nvarchar(100),@FromPWD nvarchar(100);
declare @ToInstrance nvarchar(100),@ToDB nvarchar(100),@ToPWD nvarchar(100);
declare @sql nvarchar(max),@cmd nvarchar(4000);
declare @file_table nvarchar(100);
declare @upfile_path varchar(100),@newfile_path varchar(100),@downfile_name varchar(100);
declare @error_flag int;
set nocount on;
--修改參數(shù)值
--文件名
set @upfile_path='D:\Data_bak\test.bak';--上傳文件名
set @newfile_path=@upfile_path;--預(yù)留
set @downfile_name='D:\Data_bak\test3.bak';--下載后文件名
--源服務(wù)器
set @FromInstrance = '192.168.80.1';
set @FromPWD = '123asd';
set @FromDB = 'tempdb';
--目標(biāo)服務(wù)器
set @ToInstrance = '192.168.80.130';
set @ToPWD = '123asd';
set @ToDB = 'tempdb';
--正式執(zhí)行,不要修改
set @file_table='[tmp_file_'+cast(RAND()*1000000 AS VARCHAR)+']';
print 'select *,datalength(sText) file_len from tempdb.dbo.'+@file_table;
--上傳
--1、生成upload.sql腳本(刪除表+上傳)
set @sql='if object_id(''tempdb..'+@file_table+''') is null
create table tempdb..'+@file_table+'(id int,sText nvarchar(max));
truncate table tempdb..'+@file_table+';
insert into tempdb..'+@file_table+'(id)values(1);
update tempdb..'+@file_table+' set sText=(select BULKColumn FROM OPENROWSET(BULK N'''+@newfile_path+''',SINGLE_BLOB) AS Data) where id=1;';
--print @sql;
--2、在本地釋放文件upload.sql
if object_id('tempdb.dbo.tmp_table') is not null
drop table tempdb.dbo.tmp_table;
select cast(@sql as nvarchar(max))as sText into tempdb.dbo.tmp_table;
exec master..xp_cmdshell 'bcp tempdb.dbo.tmp_table out "c:\upload.sql" -T -t"|" -w -q',no_output;
--select * from tempdb.dbo.tmp_table
--3、連接源端執(zhí)行upload.sql,文件放在源端tempdb庫(kù)
set @cmd='osql -S '+@FromInstrance+' -U sa -P '+@FromPWD+' /d '+@FromDB+' /i c:\upload.sql';
--print (@cmd);
exec master..xp_cmdshell @cmd,no_output;
--下載
--1、創(chuàng)建download.sql腳本
set @sql='exec master..xp_cmdshell ''bcp "select sText from tempdb.dbo.'+@file_table+' where ID = 1" queryout "'+@downfile_name+'" -N -q -S"'+@FromInstrance+'" -U"sa" -P"'+@FromPWD+'" '''
--print @cmd;
--2、在本地釋放文件download.sql
if object_id('tempdb.dbo.tmp_table') is not null
drop table tempdb.dbo.tmp_table;
select cast(@sql as nvarchar(max))as sText into tempdb.dbo.tmp_table;
exec master..xp_cmdshell 'bcp tempdb.dbo.tmp_table out "c:\download.sql" -T -t"|" -w -q',no_output;
--select * from tempdb.dbo.tmp_table
--3、連接源端執(zhí)行upload.sql,文件放在源端tempdb庫(kù)
set @cmd='osql -S '+@ToInstrance+' -U sa -P '+@ToPWD+' /d '+@ToDB+' /i c:\download.sql';
--print (@cmd);
exec master..xp_cmdshell @cmd,no_output;
--恢復(fù)現(xiàn)場(chǎng):刪除源端臨時(shí)表
--1、生成upload.sql腳本(刪除表)
set @sql='if object_id(''tempdb..'+@file_table+''') is not null
drop table tempdb..'+@file_table+';';
--print @sql;
--2、在本地釋放文件upload.sql
if object_id('tempdb.dbo.tmp_table') is not null
drop table tempdb.dbo.tmp_table;
select cast(@sql as nvarchar(max))as sText into tempdb.dbo.tmp_table;
exec master..xp_cmdshell 'bcp tempdb.dbo.tmp_table out "c:\upload.sql" -T -t"|" -w -q',no_output;
--select * from tempdb.dbo.tmp_table
--3、連接源端執(zhí)行upload.sql(執(zhí)行刪除表)
set @cmd='osql -S '+@FromInstrance+' -U sa -P '+@FromPWD+' /d '+@FromDB+' /i c:\upload.sql';
--print (@cmd);
exec master..xp_cmdshell @cmd,no_output;
--4、刪除本地表
if object_id('tempdb.dbo.tmp_table') is not null
drop table tempdb.dbo.tmp_table;
--5、刪除臨時(shí)文件
set @cmd='if exist c:\upload.sql del c:\upload.sql /f /s /q';
exec master..xp_cmdshell @cmd,no_output;
set @cmd='if exist c:\download.sql del c:\download.sql /f /s /q';
exec master..xp_cmdshell @cmd,no_output;
/*
--關(guān)閉高級(jí)選項(xiàng)
EXEC sp_configure 'xp_cmdshell',0
RECONFIGURE
EXEC sp_configure 'show Advanced options','0'
RECONFIGURE WITH OVERRIDE
*/
總結(jié)
以上是生活随笔為你收集整理的winxp上传文件到服务器,通过SQLServer的xp_cmdshell在服务器之间传送文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: javascript --- 函数的优
- 下一篇: javascript --- 尾递归优