我有一个java程序来监控URL状态,并检查某个公司实体的每个基于Web的应用程序的ssl证书到期日期。 jar文件部署在生产环境中。我使用了 javax.net.ssl.HttpsURLConnection 和 java.security.cert.X509Certificate 但是由于某些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确定
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确定
日期:2016年1月8日星期五17:40:07 GMT
过期:-1
Cache-Control:private,max-age = 0
Content-Type:text / html;字符集= ISO-8859-1
P3P:CP =“这不是P3P政策!有关详细信息,请参阅https://www.google.com/support/accounts/answer/151657?hl=en。”
服务器:gws
X-XSS-Protection:1;模式=块
X-Frame-Options:SAMEORIGIN
Set-Cookie:NID = 75 = Uo1kdhbBu-ZPh5mF8kYXwb-Zi-OrGCL4qk_T0eHAbrmiwvpUN1iq56D2mvgg_nN3HIadVBUe2u90a_Lk54UOXfMKHTAq2qCtNaud_cjKCogolWwP3PtX1Hm_L56uQV28NFJNRpUwZ57oWQ; expires =周六,09-Jul-2016 17:40:07 GMT;路径= /;域= .google.com;仅Http
替代协议: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
变化:接受编码
真实0.58
用户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
是
没有。 of lines:0
操作成功退出代码0
我更喜欢使用java来编写shell脚本。代码是否有任何问题,或者命令的工作方式或环境是否有任何问题。任何建议将不胜感激。
注意:这只是一个检查代码输出的示例程序。实际代码是更大包的一部分。环境是Oracle Solaris 10 1/13 s10s_u11wos_24a SPARC
答案 0 :(得分:1)
wget的--server-response
选项写入stderror然后你应该重定向stderror以便让它被认为是getInputStream()
。
这可以使用redirectErrorStream:
来实现ProcessBuilder builder = new ProcessBuilder(commandUnix);
builder.redirectErrorStream(true);
process = builder.start();
scanner = new Scanner(process.getInputStream());
答案 1 :(得分:0)
此外,我忽略了另一个简单的解决方案:在命令中添加 abduco
。
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
。