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
% 与 >>之间不能有空格,不然会把空格也算在密码中.不好
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
% 与 >>之间不能有空格,不然会把空格也算在密码中.不好