Incorrect method:
// Cpuid
Private Static final string cpuid = "dmidecode-T processor | grep 'id' | head-1 ";
PROCESS p = runtime.getruntime(cmd.exe C (puid );
Cause: it will not be parsed again, and the MPs queue is invalid.
The correct method:
In Linux:
String [] command = {"/bin/sh", "-c", (puid };
Process PS = runtime.getruntime(cmd.exe C (command );
Windows:
String [] command = {"cmd", "/C", (puid };
Process PS = runtime.getruntime(cmd.exe C (command );
There is also a way for Linux:
The "EHCO" command outputs the content in quotation marks to the standard output device. Pipeline command will be used here"|"Use the output of the [Echo] command as the input of the [OpenSSL] command.
In JavaProgram.
/** * Data encryption processing ** @ Param Data to be encrypted * @ Param Commonkey encrypted password file name * @ Return Encrypt data */ Public Static Final Synchronized String encryption (string data, string commonkey ){ // Encrypted data definition String encryptiondata = "" ; Try { // Encryption command String encryption = "echo-e \" {0} \ "| OpenSSL aes-128-cbc-e-kfile {1}-base64" ; // Replace placeholders in Commands Encryption =Messageformat. Format (encryption, Data, commonkey); string [] Sh = New String [] {"/bin/sh", "-c" , Encryption }; // Execute shell command Processbuilder Pb = New Processbuilder (SH); process P = PB. Start (); encryptiondata = Getshellout (p );} Catch (Exception e ){ Throw New Encryptionexception (E );} Return Encryptiondata ;}
Capture the output stream of shell commands in a Java program and read encrypted data
/** * Read output stream data ** @ Param P process * @ Return Data read from the output stream * @ Throws Ioexception */ Public Static Final String getshellout (PROCESS p) Throws Ioexception {stringbuilder sb = New Stringbuilder (); bufferedinputstream in = Null ; Bufferedreader br = Null ; Try {In = New Bufferedinputstream (P. getinputstream (); br = New Bufferedreader ( New Inputstreamreader (in); string S; While (S = Br. Readline ())! = Null ){ // Append a line break SB. append (constantutil. line_separator); sb. append (s );}} Catch (Ioexception e ){ Throw E ;} Finally {Br. Close (); in. Close ();} Return SB. tostring ();}