2013-04-10 8 views
5

I have a remote server name (windows), username e password.Esegui un comando in un server Windows remoto e recupera l'output della console in C# .NET

con C# .Net, voglio run a command sul server remoto e recuperare il console output

C'è un modo per farlo in C#?

Sono stato in grado di eseguire il comando utilizzando WMI con il seguente codice (parziale) ma senza fortuna di ottenere l'output della console. Potrei solo recuperare il Process ID.

ObjectGetOptions objectGetOptions = new ObjectGetOptions(); 
ManagementPath managementPath = new ManagementPath("Win32_Process"); 
ManagementClass processClass = new ManagementClass(scope, managementPath,objectGetOptions); 

ManagementBaseObject inParams = processClass.GetMethodParameters("Create"); 

inParams["CommandLine"] = "cmd.exe /c "+ mycommand; 
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null); 

Qualsiasi idea?

+0

Sto pensando lungo le linee di invocare il vostro comando tramite una sessione telnet ... Non sono sicuro che molti esperti di sicurezza sarebbero d'accordo, ma sarebbe relativamente facile da catturare l'output. – dotcomslashnet

+0

Riesci a reindirizzare l'output della console in un file di testo e recuperare il file in qualche modo? –

+0

@dotcomslashnet controllerò su quelle righe. – BlackCursor

risposta

4

Questa funzione è ciò che mi è venuto in mente dopo alcune ricerche. Spero che aiuti qualcun altro.

public string executeCommand(string serverName, string username, string password, string domain=null, string command) 
{ 
    try 
    { 
     System.Diagnostics.Process process = new System.Diagnostics.Process(); 
     System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 
     startInfo.RedirectStandardOutput = true; 
     startInfo.UseShellExecute = false; 
     startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
     startInfo.FileName = "cmd.exe"; 
     if (null != username) 
     { 
      if (null != domain) 
      { 
       startInfo.Arguments = "/C \"psexec.exe \\\\" + serverName + " -u " + domain+"\\"+username + " -p " + password + " " + command + "\""; 
      } 
      else 
      { 
       startInfo.Arguments = "/C \"psexec.exe \\\\" + serverName + " -u " + username + " -p " + password + " " + command + "\""; 
      } 
     } 
     else 
     { 
      startInfo.Arguments = "/C \"utils\\psexec.exe "+serverName+" "+ command + "\""; 
     } 
     process.StartInfo = startInfo; 
     process.Start(); 
     process.WaitForExit(); 

     if (process.ExitCode == 0 && null != process && process.HasExited) 
     { 
      return process.StandardOutput.ReadToEnd(); 
     } 
     else 
     { 
      return "Error running the command : "+command; 
     } 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
} 
1

È possibile provare a eseguire un comando con PsTools. Una delle molte funzionalità che offrono è PsExec. Ti permette di eseguire un comando su un server remoto. Dovrebbe anche restituire i risultati in una console (sul PC locale da dove è stata eseguita).

+0

Grazie ad avermi indirizzato nella giusta direzione. Ho usato con successo 'PsExec' per connettermi a un server remoto, eseguire un comando e ottenere l'output della console come stringa. Sto aggiungendo il codice come risposta separata in quanto potrebbe aiutare qualcun altro. Grazie ancora! – BlackCursor

Problemi correlati