Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Input details in cmd via Process

Input details in cmd via Process

Scheduled Pinned Locked Moved C#
helpsecuritytutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    All Time Programming
    wrote on last edited by
    #1

    Hello, I have created a Process that runs a command from cmd and handles it's output using event Handler. I want to input user name when cmd asks for "Enter Username :". This comes immediately after 1st line. Then comes Enter Password where I want to pass password. Then their is no input and can use my event handler till I am done. I tried the following, but didn't work :

            processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
            sb = new StringBuilder();
            processInfo.UseShellExecute = false;
            processInfo.RedirectStandardOutput = true;
            processInfo.CreateNoWindow = true;
        public int ConnectToServer()
        {
            processInfo.RedirectStandardInput = true;
            process = Process.Start(processInfo);
            
            process.StandardInput.WriteLine("username");
            process.StandardInput.Flush();
            process.StandardInput.WriteLine("myPswd$");
            process.StandardInput.Flush();
            process.StandardInput.Close();
    
            process.BeginOutputReadLine();
            process.OutputDataReceived += new DataReceivedEventHandler(Process\_OutputDataReceived);
        }  
    
        private void Process\_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            string d = e.Data;
            
            if (!string.IsNullOrEmpty(d))
            {
                if (d.indexOf("Completed")) {
                     connected = true;
                } 
            }
         } 
    

    It shows error authentication failed. I guess I didn't input username & passowrd on time, so this must have happened. How to know when the app is asking to write and to write proepr details ? I didn't find such way of inputting in any articles searched on internet. Any help is highly appreciated.

    Thanks & Regards,

    X P _ 3 Replies Last reply
    0
    • A All Time Programming

      Hello, I have created a Process that runs a command from cmd and handles it's output using event Handler. I want to input user name when cmd asks for "Enter Username :". This comes immediately after 1st line. Then comes Enter Password where I want to pass password. Then their is no input and can use my event handler till I am done. I tried the following, but didn't work :

              processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
              sb = new StringBuilder();
              processInfo.UseShellExecute = false;
              processInfo.RedirectStandardOutput = true;
              processInfo.CreateNoWindow = true;
          public int ConnectToServer()
          {
              processInfo.RedirectStandardInput = true;
              process = Process.Start(processInfo);
              
              process.StandardInput.WriteLine("username");
              process.StandardInput.Flush();
              process.StandardInput.WriteLine("myPswd$");
              process.StandardInput.Flush();
              process.StandardInput.Close();
      
              process.BeginOutputReadLine();
              process.OutputDataReceived += new DataReceivedEventHandler(Process\_OutputDataReceived);
          }  
      
          private void Process\_OutputDataReceived(object sender, DataReceivedEventArgs e)
          {
              string d = e.Data;
              
              if (!string.IsNullOrEmpty(d))
              {
                  if (d.indexOf("Completed")) {
                       connected = true;
                  } 
              }
           } 
      

      It shows error authentication failed. I guess I didn't input username & passowrd on time, so this must have happened. How to know when the app is asking to write and to write proepr details ? I didn't find such way of inputting in any articles searched on internet. Any help is highly appreciated.

      Thanks & Regards,

      X Offline
      X Offline
      Xmen Real
      wrote on last edited by
      #2

      how about adding username and password as command arguments eg. cmd.exe "username" "password"

      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

      ----------------------------------------------- 128 bit encrypted signature, crack if you can

      A 1 Reply Last reply
      0
      • X Xmen Real

        how about adding username and password as command arguments eg. cmd.exe "username" "password"

        TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

        ----------------------------------------------- 128 bit encrypted signature, crack if you can

        A Offline
        A Offline
        All Time Programming
        wrote on last edited by
        #3

        Thanks Dear, But no, can't add like that. Can either pass a txt file or input on screen. As I don't want to have txt file with readable username & password, so thought of this option.

        Thanks & Regards,

        1 Reply Last reply
        0
        • A All Time Programming

          Hello, I have created a Process that runs a command from cmd and handles it's output using event Handler. I want to input user name when cmd asks for "Enter Username :". This comes immediately after 1st line. Then comes Enter Password where I want to pass password. Then their is no input and can use my event handler till I am done. I tried the following, but didn't work :

                  processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
                  sb = new StringBuilder();
                  processInfo.UseShellExecute = false;
                  processInfo.RedirectStandardOutput = true;
                  processInfo.CreateNoWindow = true;
              public int ConnectToServer()
              {
                  processInfo.RedirectStandardInput = true;
                  process = Process.Start(processInfo);
                  
                  process.StandardInput.WriteLine("username");
                  process.StandardInput.Flush();
                  process.StandardInput.WriteLine("myPswd$");
                  process.StandardInput.Flush();
                  process.StandardInput.Close();
          
                  process.BeginOutputReadLine();
                  process.OutputDataReceived += new DataReceivedEventHandler(Process\_OutputDataReceived);
              }  
          
              private void Process\_OutputDataReceived(object sender, DataReceivedEventArgs e)
              {
                  string d = e.Data;
                  
                  if (!string.IsNullOrEmpty(d))
                  {
                      if (d.indexOf("Completed")) {
                           connected = true;
                      } 
                  }
               } 
          

          It shows error authentication failed. I guess I didn't input username & passowrd on time, so this must have happened. How to know when the app is asking to write and to write proepr details ? I didn't find such way of inputting in any articles searched on internet. Any help is highly appreciated.

          Thanks & Regards,

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Perhaps you're looking for this[^]?

          A 1 Reply Last reply
          0
          • P PIEBALDconsult

            Perhaps you're looking for this[^]?

            A Offline
            A Offline
            All Time Programming
            wrote on last edited by
            #5

            Thanks, but I don't want such huge code for a small task. I believe such a thing can be done with no big pain. Couple of days ago I had seen a codeo nsome site that showed stdin & stdout using Process and cmd. I searched a lot but couldn't find that site back. On my start of process, immediately after 1 line, cmd needs to input username. The code I have written doesn't let the user write anything and just goes away. When and how can the app know that it needs to write/input something. If its the 1st time then its username, if 2nd time then password. That's it. Then no input only get the output. But am not able to know when and how to know. Just saw WaitForInputIdle(). Might be that can help me. Will try with that.

            Thanks & Regards,

            1 Reply Last reply
            0
            • A All Time Programming

              Hello, I have created a Process that runs a command from cmd and handles it's output using event Handler. I want to input user name when cmd asks for "Enter Username :". This comes immediately after 1st line. Then comes Enter Password where I want to pass password. Then their is no input and can use my event handler till I am done. I tried the following, but didn't work :

                      processInfo = new ProcessStartInfo("cmd.exe", "/C " + command);
                      sb = new StringBuilder();
                      processInfo.UseShellExecute = false;
                      processInfo.RedirectStandardOutput = true;
                      processInfo.CreateNoWindow = true;
                  public int ConnectToServer()
                  {
                      processInfo.RedirectStandardInput = true;
                      process = Process.Start(processInfo);
                      
                      process.StandardInput.WriteLine("username");
                      process.StandardInput.Flush();
                      process.StandardInput.WriteLine("myPswd$");
                      process.StandardInput.Flush();
                      process.StandardInput.Close();
              
                      process.BeginOutputReadLine();
                      process.OutputDataReceived += new DataReceivedEventHandler(Process\_OutputDataReceived);
                  }  
              
                  private void Process\_OutputDataReceived(object sender, DataReceivedEventArgs e)
                  {
                      string d = e.Data;
                      
                      if (!string.IsNullOrEmpty(d))
                      {
                          if (d.indexOf("Completed")) {
                               connected = true;
                          } 
                      }
                   } 
              

              It shows error authentication failed. I guess I didn't input username & passowrd on time, so this must have happened. How to know when the app is asking to write and to write proepr details ? I didn't find such way of inputting in any articles searched on internet. Any help is highly appreciated.

              Thanks & Regards,

              _ Offline
              _ Offline
              _Erik_
              wrote on last edited by
              #6

              I think you don't need anything going asynchronous here. Remove the OutputDataReceived event handler and the invoke to BeginOutputReadLine, so you can do it synchronously. For example:

              I've not tested the code, but I guess it should work. The OutputDataReceived is only fired when a line is written on the output stream, so if there is not an end of line when it is asking for a username, you will not receive the event.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups