2013-06-20 3 views
0

xp_cmdshell을 통해 캡처 할 수있는 반환 코드를 반환하는 콘솔 응용 프로그램을 작성해야합니다. XP_CMDSHELL 반환 값을 캡처하는 방법은 무엇입니까?

class Program 
    { 
     static int Main(string[] args) 
     { 
      //make sure the correct number of arguments are being passed. 
      if (args.Length !=5) 
      { 
       Console.WriteLine("not thr right number of args. \nUsage SFTPUploadFile <host> <port> <username> <password> <localFilePath>"); 
       return 1; 
      } 

      return 0;   
     } 

    } 

XP_cmdhsell

내가 몇 가지 코드를 사용하고

다음과 같이 내가 C# 코드로 시작

, 나는

declare @rc int 

create table #output (id int identity(1,1), output nvarchar(255) null) 
insert #output (output) exec @rc = master..xp_cmdshell 'd:\FILENAME PARA1 PARA2 PARA3 PARA4 PARA5' 
select * from #output where output is not null order by id 
drop table #output 

을 발견하지만 난 내 xp_cmdshell을 실행할 때, 난 그냥 널 얻을. 1이나 0을 받아야하지 않나요?

감사

+0

아마도 'xp_cmdshell'에 대한 호출과 그 값을 확인하는 방법을 추가해야합니다. –

답변

0

그것은 당신의 프로그램이 5 개 매개 변수가 있는지 여부를 확인합니다처럼 보이는, 그리고 아무것도하지 않는다 (0을 반환)가있는 경우.

xp_cmdshell 명령은 차례대로 모든 매개 변수를 제공합니다. xp_cmdshell은 출력을받지 못하면 NULL을 반환합니다.

이 같은 것을 읽을 수있는 코드를 변경 한 경우 :

class Program 
    { 
     static int Main(string[] args) 
     { 
      //make sure the correct number of arguments are being passed. 
      if (args.Length !=5) 
      { 
       Console.WriteLine("not thr right number of args. \nUsage SFTPUploadFile <host> <port> <username> <password> <localFilePath>"); 
       return 1; 
      } 

      Console.WriteLine("You had the right number of args."); 
      return 0;   
     } 

    } 

(당신이 정말로 다시 0 또는 1을 원하는 경우 또는, Console.WriteLine("0");Console.WriteLine("1");을 할 수 있습니다.)

을 당신은 다시 얻을 것 You had the right number of args.. return 0;return 1;은 콘솔에 아무 것도 출력하지 않으므로 xp_cmdshell이 클라이언트에 다시 전송하기 때문입니다.

EXEC master..xp_cmdshell 'cd ..'을 수행하여이를 증명할 수 있습니다. 결과를 반환하지 않는 명령입니다. 반면에 EXEC master..xp_cmdshell 'dir *.exe'은 디렉토리 내용을 콘솔에 출력 (또는 쓰는 것)하기 때문에 사용자에게 반환합니다.

+0

내 로컬에 실제로 성공 또는 오류 메시지를 출력합니다. 하지만 프로덕션 서버에서는 null 또는 두 경우 모두를 반환합니다 ... 권한 문제가있을 수 있습니까? – user2206329

+0

맞습니다. xp_cmdshell은 실행을 시도 할 때 사용자 이름/암호로 실행되며, 그렇지 않으면 sysadmin입니다. 그렇지 않으면 프록시 사용자가 사용됩니다. 사용자 (또는 프록시 사용자)는 결과를보기 위해 프로덕션 환경에서 서버에 대한 올바른 사용 권한을 갖거나 갖지 않을 수 있습니다. 더 많은 것을 [여기]에서 찾을 수 있습니다 (http://msdn.microsoft.com/en-us/library/ms175046.aspx) – brazilianldsjaguar