This question already has answers here:
Bitlocker script to unlock drive
(8 answers)
Closed 6 years ago.
I am trying to unlock a drive secured by bitlocker from Java. As far as I know there are no libs which can help me to handle that, so I was trying it through cmd. Here's the code:
public static boolean unlockDisk(String pwd) throws IOException
{
String[] script =
{
"manage-bde.exe", "-unlock", "D:", "-password",
};
Process process = new ProcessBuilder(script).start();
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
final OutputStream outputStream = process.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
writer.write(pwd);
writer.newLine();
writer.close();
System.out.println("--------------------------------------");
System.out.println("Bitlocker log:");
String line;
while ((line = bufferedReader.readLine()) != null)
{
System.out.println(line);
}
bufferedReader.close();
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
System.out.println("Here is the standard error of the command (if any):\n");
String tmp;
while ((tmp = stdError.readLine()) != null)
{
System.out.println(tmp);
}
System.out.println("--------------------------------------");
return true;
}
My Problem
If I execute this java code I get The handle is invalid with Code 0x80070006.
What I already tried
Different JDK Version 32 and 64 Bit Java 8 and Java 7 (JDK 32 complains somehow that it can't find the command manage-bde)
Different combinations of output streams, with and without newline...
Another script command for the processbuilder like "cmd.exe", "/k", "manage-bde.exe", "-unlock", "D:", "-password", or with /c instead of /k
With and without admin rights
Simple *.bat with the command manage-bde.exe -unlock D: -password (which works perfectly)
Locking the drive through a java command (which works perfectly)
The command without -password (which let's bitlocker claim that I have to define how I want to unlock the drive)
I googled around for some time and found others having this problems but in a different way with other applications. So it seems like a very common error message.
My guess
I think it has something to do with how I handle my Java output as Bitlocker input. Maybe I am using the wrong streams to write to.
I can't provide the value of the password within the script variable, because Bitlocker want doesn't accept that way of entering the password. Usually you enter manage-bde -unlock D: -password within the command line and after a few lines of output Bitlocker asks you for the password.
Well I described it as good as I can and hope that someone knows what the problem is.
Any suggestion, even if it just leads to a more precise error message, would be appreciated. If you have any questions, just let me know!
Thanks in advance!
I encountered same problem recently. I did a lot search. It seems that mange-bde.exe doesn't read user input from stdin. Someone said ssh client and telent clent running on Linux doesn't read password from stdin. Another example Linux command passwd. It has a flag called -stdin which enable the shell to read password from stdin. Therefore, I guessed manage-bde.exe may works in a similar way.
My solution is simulating keyboard input. The awt package can do the job.
Related
Question:
I have to call an exe file passing 2 string arguments. The call has to get executed as a different user
I referred few of the links that I got from hints. Couple of them were using powershell, few were using runas examples and so on.
But with powershell also, I am not sure if I will face the issue of "cannot be loaded because running scripts is disabled on this system".
Just because of this reason, I want to try something authentic.
Tried following approaches
Trying to do it through a call with RunAs command is making my life difficult. (called a batch file with RunAs command passing a separate pass.txt or savecred). savecred was ruled out by most people. Other one did not work as expected.
Powershell, has issues wherein, I always get a userid/password popup, even though I run through a Java file.
I also get an error in the command prompt as here below
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodExcept
ion
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power
Shell.Commands.NewObjectCommand
(Sample code attached below)
public class SamplePS {
public static void main(String[] args) throws IOException {
Runtime runtime = Runtime.getRuntime();
String command = "powershell C:\\Users\\Ramu\\Project\\SampleFile.ps1";
//String cmds[] = {"C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", "\\c", "SampleFile.ps"};
System.out.println("1");
Process proc = runtime.exec(command);
InputStream is = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
System.out.println("2");
}
}
My ps1 file is as here below
$credential = New-Object System.Management.Automation.PSCredential <<domain>>\<<user id>>, <<password>>
start-process cmd.exe -arg "/k dir" -Credential $credential
What is the best way to handle this scenario?
Please suggest! I am not sure what approach to take.
Powershell or Perl or any other scripting.
Whatever it be, I have to make a call from Java file.
Please suggest!
Note: For time being, I dont mind the password being sent as a plain text.
The exe file is in Windows machine
Thanks!
Ram
I have a Java program running on linux that needs to be able to both set a users initial samba password, and then allow them to change their password without giving them access to the terminal.
Below is my code for changing the users password, as this is easier to test with, and I will be able to figure the other part out once I get this worked out.
The -s flag is supposed to allow stdin to be used.
String cmd = "smbpasswd -s -U user";
Process p = Runtime.getRuntime().exec(cmd);
OutputStreamWriter Out = new OutputStreamWriter(p.getOutputStream());
InputStreamReader In = new InputStreamReader(p.getInputStream());
BufferedWriter Write = new BufferedWriter(Out);
BufferedReader Read = new BufferedReader(In);
char[] output = null;
//I write all of the output lines to the log, but nothing is written, and the password doesn't change.
Read.read(output);
Write.write(OldPass);
Read.read(Output);
Write.write(NewPass);
Read.read(Output);
Write.write(NewPass);
Read.read(Output);
I need some help to figure out what I am doing wrong, and how I would go about this correctly. Any help is appreciated.
According to the man page for SMBPASSWD(8):
-s: This option causes smbpasswd to be silent (i.e. not issue prompts) and to read its old and new passwords from standard input, rather than
from /dev/tty (like the passwd(1) program does). This option is to aid
people writing scripts to drive smbpasswd
Emphasis on "not issue prompts". If I'm reading your code correctly, you seem to be waiting for prompts from the utility, which won't come (test it from the command line). But I may have misinterpreted your Java code.
This question already has answers here:
Run fortran exe in java
(3 answers)
Closed 8 years ago.
I have a Fortran exe. What I need to do.... I need to call that exe through java in Linux. After that it should ask for input file and output file.
This is my code:
Process process = new ProcessBuilder("/home/admin/Documents/file.out",
"input","output").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:", Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
It is running but nit asking for input and output file
To call external programms in java you need the java.lang.Runtime package. If you want a more convenient API have a look at Apache Commons Exec.
None of the code you have shown us "asks" anything.
I can see where you are passing two names (are they file names?) to your fortran program. But then it would be up to the fortran program to open those files and do something with them. If that's not happening, then the problem is in the fortran code ...
On the other hand, if your intention is to open the files in the Java code, and pass the file handles to the fortran program (as its standard input and standard output), then your code doesn't attempt to do that. You need to read the javadocs for ProcessBuilder. Pay attention to the stuff about redirecting input and output for the child process.
I use the following code, for redirecting the output of a process I launch from my Java app:
ProcessBuilder builder = new ProcessBuilder("MyProcess.exe");
builder.redirectOutput(Redirect.INHERIT);
builder.redirectErrorStream(true);
Now, this works fine when I run the code from eclipse - I can see the output in Eclipse's console.
Yet when I create a jar file and run it from a cmd window, e.g. java -jar MyJar.jar, it doesn't print the output of the process. What could be the reason for this?
I know I'm late in answering, but I came across this question before coming across the answer, and wanted to save anybody else in the same boat some searching.
This is actually a known bug for Windows: https://bugs.openjdk.java.net/browse/JDK-8023130
You can get around it by redirecting the streams yourself:
Process p = pb.start();
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
br.close();
It may be, that process is printing an error and exiting for some reason. So, the actual output goes into Err stream and not into the Out stream. Your code redirects Out stream only, so important process error information may be lost. I would suggest to inherit both Out and Err streams using this code:
ProcessBuilder builder = new ProcessBuilder("MyProcess.exe");
builder.inheritIO();
One more reason to redirect both streams is related to the output buffering for child process. If parent process (your java application) is not reading or redirecting standard streams (Out and Err) of the child process, then the latter may be blocked after a while, unable to make any further progress.
It definitely wouldn't hurt to have possible errors in the output anyway.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java external program
I am trying to wrtie a program to write a command on a command line,
for example;
ipconfig
and then get the response of the command so I want to both write command to a command line and get its response. I have searched about it on the net and saw that apache cli is used to do this in Java but actually I did not clearly get how it can be done. Can you please help me about my situation with a few line of codes or tutorials about both writing and reading commands please?
Thank you all very much
You could start it as a Process and capture the InputStream of the process as described here:
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("ipconfig"); // you might need the full path
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
Edit: I copied this code from above link, but the code seems wrong. Don't you need the output stream for this? Edit2: no.
getInputStream()
Gets the input stream of the subprocess. The stream obtains data
piped from the standard output stream of the process represented by
this Process object.
Nice naming convention...
See Process and ProcessBuilder classes.
Specifically, you would create a Process. Process.getOutputStream() gives an InputStream, from which you read what the process's output. You also need to read Process.getErrorStream() for any errors that the process reports.
Try this for inputting the user value.
java.util.Scanner input = new Scanner( System.in);
System.out.println("Please Enter your Name: ");
String empName = input.nextLine();