xp_cmdshell 应用

xp_cmdshell

xp_cmdshell可以让系统管理员以操作系统命令行解释器的方式执行给定的命令字符串,
并以文本行方式返回任何输出,是一个功能非常强大的扩展存贮过程。
   一般情况下,xp_cmdshell对管理员来说也是不必要的,xp_cmdshell的消除不会对Server造成
任何影响。
   可以将xp_cmdshell消除:
   Use Master
   Exec sp_dropextendedproc N’xp_cmdshell’
   Go

   如果需要的话,可以把xp_cmdshell恢复回来:
   Use Master
   Exec sp_addextendedproc N’xp_cmdshell’, N’xplog70.dll’
   Go

xp_cmdshell 操作系统命令外壳
这个过程是一个扩展存储过程,用于执行指定命令串,并作为文本行返回任何输出。
语法:
    xp_cmdshell command_string[,no_output]
        command_string    要执行的命令串
        no_output         不返回命令执行的输出
说明:
在把xp_cmdshell的执行许可权授予用户时,用户将能够在Windows NT命令
外壳执行运行SQL Server(通常是本地系统)的帐号有执行特权的任何
操作系统命令。
例:
1、EXEC master..xp_cmdshell "dir *.exe"
                             返回可执行文件的列表
2、EXEC master..xp_cmdshell "copy d:\test1.jpg e:\" no_output
                             不返回输出
3、特别是执行
EXEC master..xp_cmdshell "net start awhost32"
EXEC master..xp_cmdshell "net stop awhost32"
可以启动和停止远程的PCAnywhere服务。对远程服务器操作很有帮助。

默认情况下,sql server2005安装完后,xp_cmdshell是禁用的(可能是安全考虑),如果要使用它,可按以下步骤

-- 允许配置高级选项
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', 1
GO
-- 重新配置
RECONFIGURE
GO
-- 禁用xp_cmdshell
EXEC sp_configure 'xp_cmdshell', 0
GO
--重新配置
RECONFIGURE
GO
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值