• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implementing runAs in Java

 
Sumiran Pradhan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I need to implement the runAs functionality using Java.The problem is that runAs does not accept password in command line but as a prompt. I have a small code snippet that I tried.Can some one help me with this.

Process p = Runtime.getRuntime ( ) .exec
( "cmd /C cmd.exe" ) ;
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
Thread.sleep(1000);
while (in.ready()) {

String x = in.readLine();
System.out.println("The value is :::" + x);
if(x.contains("password"))
{
Thread.sleep(1000);
System.out.println("Inside loop...");
//OutputStreamWriter op = new OutputStreamWriter(p.getOutputStream());
String cmd="global@123";
p.getOutputStream().write(cmd.getBytes());
p.getOutputStream().flush();
//op.write(cmd, 0, cmd.length());
//op.flush();
Thread.sleep(1000);
}
}
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags. You can edit your post to add them.
 
Sumiran Pradhan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You dont say what problem you encountered so I am going to take a guess.

Carefully review the JavaDocs for java.lang.Process

Pay close attention to the need for consuming the stdOut and stdErr streams generated by the process.

This sort of problem comes up frequently with exec() so search this forum.

Bill
 
Sumiran Pradhan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My problem is that I am unable to enter the password through my Java Code for runAs implementation.I have tried the same on a single thread.Then I read this article in the forum regarding consuming error streams and input streams.Here is what I tried and its still not working.


Output is :->
Execing cmd.exe /C runAs /user:testAdmin notepad.exe
OUTPUT>Enter the password for bmcadmin:
ExitValue: 1

Now Exit Value 1 would mean an error but I am unable to debug as to what is the problem in this case.Ne help guys?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems to me that runAs is expecting to conduct a conversation - shouldn't you be reading the output and responding with the password after it is requested.

Bill
 
Sumiran Pradhan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay guys , again I have modified the code and I am still unable to do what I set about doing. I need to implement the runAs command through Java.The problem is that it prompts for a password which one cannot provide in the runAs command.Any help would be great.

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For one thing:


Reads a "line" as defined by a line terminator - presumably \n BUT the prompt does NOT have a terminator.

I suggest you start fresh, think about what has to be recognized in the process output and the sequence of events. I would even draw some sort of state diagram showing the various states the process can be in and the transitions.

You may well end up with several separate objects run by separate Threads.

Those monster methods are not making good use of Java and you are getting all balled up trying to cram everything into them.

Bill
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The password is read from the raw console device, and doesn't go through stdin at all. Anything along the lines of what's been suggested here will never work.

Here are a few pages that describe available workarounds:

http://blogs.msdn.com/oldnewthing/archive/2004/11/29/271551.aspx
http://www.tek-tips.com/faqs.cfm?fid=2760
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic