locked
Open a Command Prompt of Remote Computer RRS feed

  • Question

  • User-456111751 posted

    Hi All,

    I need to open a Command prompt (cmd.exe) of a Remote server computer and Run a Command from my asp.net web application (C# laungauge). 

    I have progressed till opening a command prompt of the webserver where the website is hosted. My code is:

    System.Diagnostics.ProcessStartInfo psi = new ProcessStartInfo();
            
            psi.WorkingDirectory = "C:\\WINDOWS\\system32";
            psi.FileName = "cmd.exe"; 
            Process p = Process.Start(psi);  
            System.Threading.Thread.Sleep(5000);
    


    I have achived this using the VB script. The code is:

    wshShell.run("c:\scripts\uptime\psexec.exe \\nawinapp627 -u domainname\userid -p password cmd.exe")
    	WScript.Sleep 5000

    In the above code psexec.exe is software used to access remote computers.

    nawinapp627 = Remote server

    Please help me to achieve above using C#.

    Advance thanks..

    Friday, September 3, 2010 6:54 AM

Answers

  • User-1852462877 posted

    Put this on your button click event. My button name is Button1

        protected void Button1_Click(object sender, EventArgs e)
        {
           System.Diagnostics.ProcessStartInfo  psi  =  new ProcessStartInfo();
            psi.WorkingDirectory = "C:\\WINDOWS\\system32";
            psi.FileName = "cmd.exe";
            psi.Arguments = " /C notepad";
            var p = Process.Start(psi);
            System.Threading.Thread.Sleep(5000);
        }


    It will open Notepad after the CMD prompt. You close Notepad, CMD will exit. Check the above posted link...

    See Ya!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 3, 2010 3:30 PM
  • User-2004844803 posted

    check this page:

    http://generally.wordpress.com/2007/05/31/a-simple-remoting-example-in-c/

    I know, now you all think "what a moron, he should use WCF for that!". Sure, I agree. WCF is one way remoting another.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 7, 2010 7:06 AM

All replies

  • User-1659704165 posted

    Hi,

    If u want to run cmd proment on remote computer u will Require activex for That.
    if try Vbscript or javascript .. It ma run some where and Not some where.

    and more over it is Not Good idea such as Init the CMD on client computer

    Friday, September 3, 2010 9:13 AM
  • User-1852462877 posted

    Have you tried the /C option???

    Just add something like:

    psi.Arguments = " /C ""notepad"""

    before starting the process.

    You can have a look at more options on: http://ss64.com/nt/cmd.html

    See Ya!

    Friday, September 3, 2010 10:12 AM
  • User-456111751 posted

    It works with VB Script, but i do want to achieve this using C#. Any help appreaciated 

    Friday, September 3, 2010 12:22 PM
  • User-1852462877 posted

    Put this on your button click event. My button name is Button1

        protected void Button1_Click(object sender, EventArgs e)
        {
           System.Diagnostics.ProcessStartInfo  psi  =  new ProcessStartInfo();
            psi.WorkingDirectory = "C:\\WINDOWS\\system32";
            psi.FileName = "cmd.exe";
            psi.Arguments = " /C notepad";
            var p = Process.Start(psi);
            System.Threading.Thread.Sleep(5000);
        }


    It will open Notepad after the CMD prompt. You close Notepad, CMD will exit. Check the above posted link...

    See Ya!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 3, 2010 3:30 PM
  • User-456111751 posted

    Hi Candor,

    Thanks for your reply. What i need to achieve is to open a command prompt and to execute to command on a remote server (Its different from the webserver). Then i need to copy the output of the command executed from the command prompt to a web page.

    Any help please...

    Saturday, September 4, 2010 11:31 AM
  • User-1852462877 posted

    A user browse's your website, your website executes some command on that user's PC, the result are sent back to you?

    What you are asking is unacceptable!


    You wont be allowed to execute command on a remote machine.
    If that would be possible, then hackers would have hacked each and every PC ever connected to the internet.
    The new browser's are very carefully designed, I dont think any of the browser would allow you to run executables on their client.
    But I have heard of executing some limited commands with the use of ActiveX, but then this would only limited to IE browser.
    Still if it could accomplish your purpose you should try searching more about it.
    Let me give you an example: Have you ever encountered a website (in IE) that asks you to install an activex (dll) component?
    Well, if not then there are many such websites. e.g. Microsoft Update website (in IE) checks if your windows is pirated/original, through an activex component! FYI: If you are opening it in FF then it will ask you to download an exe and that exe will give you a code to fill in the update download form.

    Saturday, September 4, 2010 12:07 PM
  • User-456111751 posted

    Thanks CandorZ for your guidance. I have already acheived this using VB Script with an account which has got permission to run a command on a command prompt.

    My new requirement is to get the same stuff using my asp.net website. I am trying this out.

    Thanks again 

    Saturday, September 4, 2010 12:42 PM
  • User-456111751 posted

    Hi Candor,

    Finally I got it working for connecting to Remote comouter with the tool PSEXEC.exe. Every thing works fine on the VS 2005 IDE but after moving it to IIS 6.0, I am not getting the IP addressof the remote machine. I have given full control permission the website folder and it has got admin privelige. Please find the code:

     

    Process p = new Process();
            string result = "";
    
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
    
            //p.StartInfo.WorkingDirectory = "C:";
            p.StartInfo.UserName = @"ashimo01";
    
            SecureString pwd = new SecureString();
            pwd.AppendChar('P');
            pwd.AppendChar('a');
            pwd.AppendChar('s');
            pwd.AppendChar('s');
            pwd.AppendChar('w');
            pwd.AppendChar('o');
            pwd.AppendChar('r');
            pwd.AppendChar('d');
            pwd.AppendChar('1');
    
            // cannot modify it after making it read only (secure!)
            pwd.MakeReadOnly();
    
    
            p.StartInfo.Password = pwd;
            p.StartInfo.FileName = @"C:\psexec.exe";
            p.StartInfo.Arguments = @"-u enterprise\ashimo01 -p Password1 \\nawrkxp613 cmd.exe /c ipconfig"; 
    
            p.Start();
            //System.Threading.Thread.Sleep(5000);
    
            StreamReader stdOut = p.StandardOutput;
            p.WaitForExit();
    
            result = stdOut.ReadToEnd();
    
            if (!p.HasExited)
            {
                p.Kill();
            }
    
    
            stdOut.Close();
            p.Close();
            //You can parse the result
            Response.Write(result);
            p.Close();


     

    Tuesday, September 7, 2010 5:57 AM
  • User-456111751 posted

    This is Something related to IIS settings. And idea on what will that be? 

    Tuesday, September 7, 2010 6:12 AM
  • User-2004844803 posted

    hi ashigf,

    I have no direct answer to your question but I would try to create a service that starts the process and install that service on your remote machine. you can then send messagez to this service from your aspnet application using techniqes like remoting. I have solved similar problems where all my web servers was talking to a single logging machine using remoting with great success.

    hope this helps.

    Tuesday, September 7, 2010 6:37 AM
  • User-456111751 posted

    I would try to create a service that starts the process and install that service on your remote machine

    Thanks for your reply. Please help me on creating a Service on Remoting coz I hav'nt worked with that or share some projects or tutorial. I guess that there would be some settings which i have to set on my IIS for my above code to work...

    Others: Any help on my IIS settings

    Tuesday, September 7, 2010 6:54 AM
  • User-2004844803 posted

    check this page:

    http://generally.wordpress.com/2007/05/31/a-simple-remoting-example-in-c/

    I know, now you all think "what a moron, he should use WCF for that!". Sure, I agree. WCF is one way remoting another.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 7, 2010 7:06 AM
  • User-79055209 posted

    Hi all,

    Is there any possibility of accessing the remote machine's command prompt, execute a command, and getting it back to our console without using the PSEXEC.exe ?? Since it is not working with my machine when i tried it with psexec.exe. My machine is not supporting that functionality..

    Expecting speedy replies..

    Thanks in advance.

     

    Sridhar Narasimhan

    Friday, September 17, 2010 3:02 AM