Showing posts with label likeexec. Show all posts
Showing posts with label likeexec. Show all posts

Monday, March 26, 2012

question on using xp_cmdshell

I am trying to run an exe file like:

EXEC master.dbo.xp_cmdshell '\\Share\sharepoint\folder1\folder2\test.exe "input parameter"'

And executing the above with the input should give me an output.But the input parameter is not recognised. Any help.

Thanks

EXEC xp_cmdshell 'dir *.exe';

try removing the double quotes, above I do a DIR command passing in *.exe

|||Not recognized by whom? The test.exe command? What do you mean by input parameter. Can you post an actual example so we can try it?|||EXEC master.dbo.xp_cmdshell '\\Share\sharepoint\folder1\folder2\test.exe input_parameter'|||Did these replies solved your problem or not?|||

The test.exe is a tool to extract output when an input is given.

EXEC master.dbo.xp_cmdshell '\\Share\sharepoint\folder1\folder2\test.exe input'

if i do like above i should be able to get the output.

Thanks

|||

kast wrote:

The test.exe is a tool to extract output when an input is given.

EXEC master.dbo.xp_cmdshell '\\Share\sharepoint\folder1\folder2\test.exe input'

if i do like above i should be able to get the output.

Thanks

good, then mark a reply as the ANSWER.

Derek

|||

There is one problem still.

On the command prompt

I use as C:\folderA\folderB\test.exe "input" gives me the ouput desired.

When I do same thing in the query analyszer using xp_cmdshell as

EXEC master.dbo.xp_cmdshell 'C:\folderA\folderB\test.exe "input"'

it errors out saying

NULL
Error: c:\folderA\exam.cpp (100):Failed to open the key file
Error: c:\folderA\exam2.cpp (150):Invalid input
NULL

I checked and the relavant cpp files are in that path and also this error doesn't happen when i do in the cmd prompt directly.

Anything wrong with the xp_cmdshell syntax I have used?

Thanks

|||Does the key file exist on the path C:\folderA on the server? Note that the xp_cmdshell runs on the server so you need to copy all the necessary files for the EXEC to the corresponding directories on the server. There is nothing wrong with the xp_cmdshell syntax. The problem is in your environment / configuration.|||

One of two things are happening.

1. The files are not in the location expected

2. You have a security problem

When you execute it directly from a command line, the security context it will be running under is your Windows account and it will also be using the path statement set for your account to locate files.

When you execute this using xp_cmdshell, the security context the command is running under is the Windows account of the SQL Server service and also be using the path statement set for that account. The the SQL Server service account does not have the authority to open the key file this .exe is directly things through, then you are going to get this type of error.

|||

Hello

It seems like you are having a security issue here.

When you are using xp_cmdshell then you will "loose" your security context, and SQL Server will try to create a connection to the share with HIS security context. If you use a "Local Systemaccount" for SQL Server it wont be possible to access any network shares in your domain, since this account is "unknown" to the domain. So either move the file to a harddrive thats local to the SQL Server, or switch the SQL Server to use a domain account (I would prefer moving the file locally, since granting the SQL Server domain access would open attackers entry to your domain if they manage to compromise your SQL Server)

|||

EXEC master.dbo.xp_cmdshell 'C:\folderA\folderB\test.exe "input"'

Instead of doing as above I created a test.bat file as

cd\
cd folderA
cd folderB

Test.exe "input"

and then called it as

EXEC master.dbo.xp_cmdshell Test.bat "input"

This Worked.Thanks everyone for the effort.Thanks again.

|||

you may want to note that XPs are to no longer exist after SQL 2005 via this quote that is everywhere in BOL2005:

"This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use CLR Integration instead."

And regardless of xp_cmdShells future I would encourage you to port what your doing into the CLR as it is a much more safer and secure environment than xps. As a matter of fact in my upcoming book we have an entire chapter dedicated to the topic as porting XPs is one of the big reasons to employ the technology.

Derek

|||Thanks for the information.|||Please mark an answer