wget 不通过 Runtime.getRuntime.exec(String[] commandArray) 显示输出

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

我有一个 java 程序来监视 URL 状态并检查某个公司实体的每个基于 Web 的应用程序的 ssl 证书到期日期。 jar 文件部署在生产环境中。我使用了 javax.net.ssl.HttpsURLConnectionjava.security.cert.X509Certificate 但是它们似乎并不适用于所有 URL这是由于某些 VPN 或内部网问题,但是可以在生产服务器本身中通过 运行 shell 命令检查 URL 的状态。由于未安装 openssl 我通过使用

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

回应

HTTP/1.1 200 OK

RESPONSE_CODE: 200

RESPONSE_TIME: 0.293

但这也不适用于所有网站,因此经过反复试验后我开始使用

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

响应,

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

下面给出了测试程序和输​​出结果,但是wget似乎没有显示结果,尽管它已成功执行;而带有 curl 命令的类似程序显示结果。

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

      }

}

运行

上的输出

java RunProcessWGET www.google.com

No. of Lines: 0

Operation Successful with exit code 0

与 shell 相比,我更喜欢使用 java 脚本。代码、命令的工作方式或环境是否有任何问题。任何建议将不胜感激。


注意:这只是一个示例程序,用于检查代码的输出。实际代码是更大包的一部分。环境是,Oracle Solaris 10 1/13 s10s_u11wos_24a SPARC

wget 的 --server-response 选项写入 stderr 然后你应该重定向 stderr 以便让它认为 getInputStream()

这可以使用 redirectErrorStream 来实现:

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

我还忽略了另一个简单的解决方案:将 2>&1 添加到命令中。

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"};

基本上做同样的事情,将 stderr 重定向到 stdout.