简体   繁体   中英

wget not displaying output through Runtime.getRuntime.exec(String[] commandArray)

I have a java program to monitor URL status and check ssl certificate expiry date for every web based application for a certain corporate entity. The jar file is deployed in a production environment. I have used javax.net.ssl.HttpsURLConnection and java.security.cert.X509Certificate however they do not seem to work for all URLs due to some VPN or intranet issue, however it is possible to check the status of the URL by running shell commands in the production server itself. Since openssl is not installed I get by using

curl -Iks -w "RESPONSE_CODE: %{http_code}\\nRESPONSE_TIME: %{time_total}\\n" https://www.google.com | egrep 'HTTP|RESPONSE'

the response

HTTP/1.1 200 OK

RESPONSE_CODE: 200

RESPONSE_TIME: 0.293

but this also doesn't work for all websites, hence after some trial and error I started using

time -p wget --spider --connect-timeout=20 --read-timeout=50 --no-check-certificate --server-response --quiet https://www.google.com

the response,

HTTP/1.1 200 OK

Date: Fri, 08 Jan 2016 17:40:07 GMT

Expires: -1

Cache-Control: private, max-age=0

Content-Type: text/html; charset=ISO-8859-1

P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."

Server: gws

X-XSS-Protection: 1; mode=block

X-Frame-Options: SAMEORIGIN

Set-Cookie: NID=75=Uo1kdhbBu-ZPh5mF8kYXwb-Zi-OrGCL4qk_T0eHAbrmiwvpUN1iq56D2mvgg_nN3HIadVBUe2u90a_Lk54UOXfMKHTAq2qCtNaud_cjKCogolWwP3PtX1Hm_L56uQV28NFJNRpUwZ57oWQ; expires=Sat, 09-Jul-2016 17:40:07 GMT; path=/; domain=.google.com; HttpOnly

Alternate-Protocol: 443:quic,p=1

Alt-Svc: quic="www.google.com:443"; ma=600; v="30,29,28,27,26,25",quic=":443"; ma=600; v="30,29,28,27,26,25"

Transfer-Encoding: chunked

Accept-Ranges: none

Vary: Accept-Encoding

real 0.58

user 0.27

sys 0.01

the test program is given below along with the output, however wget doesn't seem to display the result, although it is successfully executed; while a similar program with curl command displays the result.

import java.io.IOException;
import java.util.Scanner;

public class RunProcessWGET {

    public static void main(String[] args) {

            Process process = null;
            Scanner scanner = null;
            int lineCount = 0;
            int exitValue = 0;
            String[] commandUnix = {"/bin/sh",
                "-c",
                "time -p wget --spider --connect-timeout=20 --read-timeout=50 --no-check-certificate --server-response --quiet \""
                +args[0]+"\""};
            try {
                    process = Runtime.getRuntime().exec(commandUnix);
                    scanner = new Scanner(process.getInputStream());
                    while (scanner.hasNext()) {
                            ++lineCount;
                            System.out.println(scanner.nextLine());
                    }
                    System.out.println("No. of Lines: "+lineCount);
                    process.waitFor();
                    exitValue = process.exitValue();
                    System.out.println((exitValue == 0?
                            "Operation Successful with exit code 0" : ("Operation Failed with exit code "+exitValue)));
                    if(exitValue != 0) {
                            scanner = new Scanner(process.getErrorStream());
                            while (scanner.hasNext()) {
                                    System.out.println(scanner.nextLine());
                            }
                    }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

      }

}

the output on running

java RunProcessWGET www.google.com

is

No. of Lines: 0

Operation Successful with exit code 0

I prefer to use java to shell scripts. Is there any problem with the code, or with the way the command works or the environment. Any suggestions will be appreciated.


NOTE: This is only a sample program to check the output of the code. The actual code is a part of a larger package. Environment is, Oracle Solaris 10 1/13 s10s_u11wos_24a SPARC

The --server-response option of wget write to stderror then you should redirect stderror in order to get it thought getInputStream() .

This can be achieved using redirectErrorStream :

ProcessBuilder builder = new ProcessBuilder(commandUnix);
builder.redirectErrorStream(true);
process = builder.start();
scanner = new Scanner(process.getInputStream());

Also I overlooked another simple solution: appending 2>&1 to the command.

String[] commandUnix = {"/bin/sh",
                "-c",
                "time -p wget --spider --connect-timeout=20 --read-timeout=50 --no-check-certificate --server-response --quiet \""
                +args[0]+"\" 2>&1"};

Basically does the same thing, redirects stderr to stdout .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM