Execute Linux statements through java in the server

Execute Linux statements through java in the server


Here I only introduce one kind of application and will not analyze the function function in detail.
Requirements: The specified file is encrypted des3
not the same place with the simple statement is executed, this statement is used in the encryption pipe symbol can not be used directly below this statement

Process process=Runtime.getRuntime().exec("ls ./");

I wrote the code like this. Note that the parameter in exec is new String[]{"sh","-c",com}

public static void main(String[] args) {
    
    
        String com = "tar -zcvf -834_Contentviewlog.txt | openssl des3 -salt -k " +
                "dTVu98DF | dd of=Contentviewlog.txt.des3";
        String com2 = "ps -ef";
        //execute(com);
        String com3 = "tar -zcvf 834_Contentviewlog.txt.tar.gz /834_Contentviewlog.txt";
        InputStream in = null;
        try {
    
    
            Process pro = Runtime.getRuntime().exec(new String[]{
    
    "sh","-c",com3});
            pro.waitFor();
            in = pro.getInputStream();
            BufferedReader read = new BufferedReader(new InputStreamReader(in));
            String result = read.readLine();
            System.out.println("INFO:"+result);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }

The statement without the pipe symbol can be executed successfully, so I didn't go into it, so I will talk about it first.

Guess you like

Origin blog.csdn.net/zhuyin6553/article/details/108482865