BCP_XP_SHELL_FTP_UPDLOAD

xp_cmdshell
bcp


ftpupload




最近公司有一个单子,大致是把查数据库的数据导出,再上传到某FTP服务器(至少一开始我是这样想的)


有几点我是需要查询的知识.
xp_cmdshell


下面一段转载
-----------------------------------------------------------------------------------

 xp_cmdshell 扩展存储过程将命令字符串作为操作系统命令 shell 执行,并以文本行的形式返回所有输出。由于xp_cmdshell 可以执行任何操作系统命令,所以一旦SQL Server管理员帐号(如sa)被攻破,那么攻击者就可以利用xp_cmdshell 在SQL Server中执行操作系统命令,如:创建系统管理员,也就意味着系统的最高权限已在别人的掌控之中。由于存在安全隐患,所以在SQL Server 2005中, xp_cmdshell 默认是关闭的。 


  两种方式启用xp_cmdshell 


  1.打开外围应用配置器—> 


  功能的外围应用配置器—> 


  实例名Database Enginexp_cmdshell—> 


  启用 


  2.sp_configure 


  -- 允许配置高级选项 


  EXEC sp_configure 'show advanced options', 1 


  GO 


  -- 重新配置 


  RECONFIGURE 


  GO 


  -- 启用xp_cmdshell 


  EXEC sp_configure 'xp_cmdshell', 1 


  GO 


  --重新配置 


  RECONFIGURE 


  GO 


  --执行想要的xp_cmdshell语句 


  Exec xp_cmdshell 'query user' 


  GO 


  --用完后,要记得将xp_cmdshell禁用(从安全角度安全考虑) 


  -- 允许配置高级选项 


  EXEC sp_configure 'show advanced options', 0 


  GO 


  -- 重新配置 


  RECONFIGURE 


  GO 


  -- 禁用xp_cmdshell 


  EXEC sp_configure 'xp_cmdshell', 0 


  GO 


  --重新配置 


  RECONFIGURE 


  GO
---------------------------------------------------------------------------------


能用sql中的xp_shell之后还要用到bcp去作导出数据,
其中最蛋痛的某过于用"|"作为分隔符了,
因为有需求中要用到|作为分隔...
而在dos的shell中,刚好|又有另外的含义,我弄了老半天才发现可以用双引号作为转义.不是单引号
不是back slash,
最后基本的库表为
bcp "select s.stdstationID, os.object_name, o.current_value from viewfocus_nb_20120218.dbo.m_station as s join viewfocus_nb_20120218.dbo.m_object as o on s.object_id = o.station_id join viewfocus_nb_20120218.dbo.m_object as os on s.object_id = os.object_id where o.object_name ='ccc'and o.lsc_id = (select net_id from viewfocus_nb_20120218.dbo.m_property where vendor_name like '%BS%' )"  queryout  d:\20120228.txt -c -t"|" -S10.3.40.130 -Usa -Pdata130






另外,发现在windows下用ftp客户端去做一些脚本也提好玩的...


下面是脚本的一段,以作记录
----------------------------------------------------------------------------------------


:下面代码中红色的部分需要换成你自己的设置
:   @set server=www.livebaby.cn   -- 你服务器的ftp地址

:@set username=anonymous   -- 你服务器的ftp的用户名@set password=anonymous   -- 你服务器的ftp的用户密码
:@set localpath=d:\tool              --你本地上传文件所在的目录




:ftp_upload.bat


@if "%1"=="" goto error_parm
@if "%2"=="" goto error_parm


@setlocal
@set server=10.3.30.111
@set username=root
@set password=command
@set localpath=e:\tmp


@echo off
@echo /******************************/ >>upload.log
@echo %date% %time% >>upload.log
@echo upload file start >>upload.log


if not exist %localpath%\%2 goto error_dir_local


echo open %server%> ftp.txt
echo user %username%>> ftp.txt
echo %password%>> ftp.txt
:echo cd %1 >> ftp.txt
echo bye>> ftp.txt
ftp -s:ftp.txt -n | find "550"
if not errorlevel 1 @goto error_dir_server




echo open %server%> ftp.txt
echo user %username%>> ftp.txt
echo %password%>> ftp.txt
:echo cd %1 >> ftp.txt
echo put %localpath%\%2 >> ftp.txt
echo bye>> ftp.txt
ftp -s:ftp.txt -n | find "invalid" 
if not errorlevel 1 @goto error_upload


@echo off
@echo upload file completed ! >>upload.log
@goto putok 


:putok
@echo upload %2 successflu ! >>upload.log
@goto end


:error_dir_server
@echo server directory error! %1  >>upload.log
@goto end


:error_dir_local
@echo local directory error! %localpath%\%2 >>upload.log
@goto end


:error_upload
@echo upload file error! %localpath%\%2 >>upload.log
@goto end


:error_parm
@echo parameter error(upload.bat   serverpath   localfilename) >>upload.log
@goto end


:end
@echo bye. >>upload.log


@endlocal


----------------------------------------------------------------------------------------
要注意的一点就是在输入账号及密码那一段


echo user %username%>> ftp.txt
echo %password%>> ftp.txt


% 与 >>之间不能有空格,不然会把空格也算在密码中.不好


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值