Java runtime.getruntime(cmd.exe C call the system script/command precautions

Source: Internet
Author: User
Tags openssl aes

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 ();} 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.