CloseableHttpClient类httpclient方法执行linux命令报错

shirui8653719 2016-08-03 10:38:55
我用httpclient方法执行linux命令时,有些命令可以执行并返回结果,有些命令则返回报错的结果,比如:
我输入‘date’命令,打印出的结果是:{"result":"Wed Aug 3 10:19:17 CST 2016\n","resultCode":"0"},
而我输入查询端口的命令,‘netstat -tln’,返回的结果就是报错信息,如最下面的红色信息,谁知道这是什么原因,如果我想执行查询端口的命令,应该怎么写,或是添加什么变量?

package touda_web;

import java.io.IOException;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;

public class HttpClientTest {

public static void main(String[] args) {
Map<String, String> body = Maps.newHashMap();
body.put("command", "netstat -tln");// 返回报错信息
//body.put("command", "date");// 返回正确信息: {"result":"Wed Aug 3 10:19:17 CST 2016\n","resultCode":"0"}

body.put("output", "true");
Map<String, Map<String, String>> requestMap = Maps.newHashMap();
requestMap.put("Body", body);
String result = sendRequest("/exec", "182.180.80.101", requestMap);
System.out.println(result);

}

public static String sendRequest(String path, String ip,
Map<String, Map<String, String>> requestMap) {

CloseableHttpClient httpclient = null;
try {
httpclient = HttpClients.createDefault();

String port = System.getProperty("httpPort");
if (Strings.isNullOrEmpty(port)) {
port = "8787";
}
HttpPost post = new HttpPost("http://" + ip + ":" + port + path);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(5000).setConnectTimeout(5000).build();// 设置请求和传输超时时间
post.setConfig(requestConfig);

StringEntity entity = new StringEntity(
JSON.toJSONString(requestMap));
post.setEntity(entity);
HttpResponse response = httpclient.execute(post);
HttpEntity resEntity = response.getEntity();
if (response.getStatusLine().getStatusCode() != 200) {
//System.out.println("The responseCode of {} is {} "+post.getURI()+response.getStatusLine().getStatusCode());
return null;
}
String responseStr = EntityUtils.toString(resEntity, "utf-8");
EntityUtils.consume(resEntity);
return responseStr;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Failed to sendPublishRequest");
} finally {
try {
httpclient.close();
} catch (IOException e) {
// ignore
}
}
return null;

}
}


下面为报错信息
{"error":{"@type":"java.util.concurrent.ExecutionException","cause":{"@type":"java.io.IOException","cause":{"@type":"java.io.IOException","localizedMessage":"error=2, No such file or directory","message":"error=2, No such file or directory","stackTrace":[{"className":"java.lang.UNIXProcess","fileName":"UNIXProcess.java","lineNumber":-2,"methodName":"forkAndExec","nativeMethod":true},{"className":"java.lang.UNIXProcess","fileName":"UNIXProcess.java","lineNumber":186,"methodName":"<init>","nativeMethod":false},{"className":"java.lang.ProcessImpl","fileName":"ProcessImpl.java","lineNumber":130,"methodName":"start","nativeMethod":false},{"className":"java.lang.ProcessBuilder","fileName":"ProcessBuilder.java","lineNumber":1028,"methodName":"start","nativeMethod":false},{"className":"java.lang.Runtime","fileName":"Runtime.java","lineNumber":617,"methodName":"exec","nativeMethod":false},{"className":"java.lang.Runtime","fileName":"Runtime.java","lineNumber":485,"methodName":"exec","nativeMethod":false},{"className":"com.pccc.touda.vmclient.http.impl.ExecRunner","fileName":"ExecRunner.java","lineNumber":46,"methodName":"call","nativeMethod":false},{"className":"com.pccc.touda.vmclient.http.impl.ExecRunner","fileName":"ExecRunner.java","lineNumber":1,"methodName":"call","nativeMethod":false},{"className":"java.util.concurrent.FutureTask","fileName":"FutureTask.java","lineNumber":262,"methodName":"run","nativeMethod":false},{"className":"java.util.concurrent.ThreadPoolExecutor","fileName":"ThreadPoolExecutor.java","lineNumber":1145,"methodName":"runWorker","nativeMethod":false},{"className":"java.util.concurrent.ThreadPoolExecutor$Worker","fileName":"ThreadPoolExecutor.java","lineNumber":615,"methodName":"run","nativeMethod":false},{"className":"java.lang.Thread","fileName":"Thread.java","lineNumber":745,"methodName":"run","nativeMethod":false}],"suppressed":[]},"localizedMessage":"Cannot run program \"netstat -tln\": error=2, No such file or directory","message":"Cannot run program \"netstat -tln\": error=2, No such file or directory","stackTrace":[{"className":"java.lang.ProcessBuilder","fileName":"ProcessBuilder.java","lineNumber":1047,"methodName":"start","nativeMethod":false},{"className":"java.lang.Runtime","fileName":"Runtime.java","lineNumber":617,"methodName":"exec","nativeMethod":false},{"className":"java.lang.Runtime","fileName":"Runtime.java","lineNumber":485,"methodName":"exec","nativeMethod":false},{"className":"com.pccc.touda.vmclient.http.impl.ExecRunner","fileName":"ExecRunner.java","lineNumber":46,"methodName":"call","nativeMethod":false},{"className":"com.pccc.touda.vmclient.http.impl.ExecRunner","fileName":"ExecRunner.java","lineNumber":1,"methodName":"call","nativeMethod":false},{"className":"java.util.concurrent.FutureTask","fileName":"FutureTask.java","lineNumber":262,"methodName":"run","nativeMethod":false},{"className":"java.util.concurrent.ThreadPoolExecutor","fileName":"ThreadPoolExecutor.java","lineNumber":1145,"methodName":"runWorker","nativeMethod":false},{"className":"java.util.concurrent.ThreadPoolExecutor$Worker","fileName":"ThreadPoolExecutor.java","lineNumber":615,"methodName":"run","nativeMethod":false},{"className":"java.lang.Thread","fileName":"Thread.java","lineNumber":745,"methodName":"run","nativeMethod":false}],"suppressed":[]},"localizedMessage":"java.io.IOException: Cannot run program \"netstat -tln\": error=2, No such file or directory","message":"java.io.IOException: Cannot run program \"netstat -tln\": error=2, No such file or directory","stackTrace":[{"className":"java.util.concurrent.FutureTask","fileName":"FutureTask.java","lineNumber":122,"methodName":"report","nativeMethod":false},{"className":"java.util.concurrent.FutureTask","fileName":"FutureTask.java","lineNumber":202,"methodName":"get","nativeMethod":false},{"className":"com.pccc.touda.vmclient.http.impl.ExecHandler","fileName":"ExecHandler.java","lineNumber":41,"methodName":"handler","nativeMethod":false},{"className":"com.pccc.touda.vmclient.http.HttpHandler","fileName":"HttpHandler.java","lineNumber":67,"methodName":"handle","nativeMethod":false},{"className":"org.eclipse.jetty.server.handler.HandlerWrapper","fileName":"HandlerWrapper.java","lineNumber":116,"methodName":"handle","nativeMethod":false},{"className":"org.eclipse.jetty.server.Server","fileName":"Server.java","lineNumber":370,"methodName":"handle","nativeMethod":false},{"className":"org.eclipse.jetty.server.AbstractHttpConnection","fileName":"AbstractHttpConnection.java","lineNumber":494,"methodName":"handleRequest","nativeMethod":false},{"className":"org.eclipse.jetty.server.AbstractHttpConnection","fileName":"AbstractHttpConnection.java","lineNumber":982,"methodName":"content","nativeMethod":false},{"className":"org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler","fileName":"AbstractHttpConnection.java","lineNumber":1043,"methodName":"content","nativeMethod":false},{"className":"org.eclipse.jetty.http.HttpParser","fileName":"HttpParser.java","lineNumber":865,"methodName":"parseNext","nativeMethod":false},{"className":"org.eclipse.jetty.http.HttpParser","fileName":"HttpParser.java","lineNumber":240,"methodName":"parseAvailable","nativeMethod":false},{"className":"org.eclipse.jetty.server.AsyncHttpConnection","fileName":"AsyncHttpConnection.java","lineNumber":82,"methodName":"handle","nativeMethod":false},{"className":"org.eclipse.jetty.io.nio.SelectChannelEndPoint","fileName":"SelectChannelEndPoint.java","lineNumber":696,"methodName":"handle","nativeMethod":false},{"className":"org.eclipse.jetty.io.nio.SelectChannelEndPoint$1","fileName":"SelectChannelEndPoint.java","lineNumber":53,"methodName":"run","nativeMethod":false},{"className":"org.eclipse.jetty.util.thread.QueuedThreadPool","fileName":"QueuedThreadPool.java","lineNumber":608,"methodName":"runJob","nativeMethod":false},{"className":"org.eclipse.jetty.util.thread.QueuedThreadPool$3","fileName":"QueuedThreadPool.java","lineNumber":543,"methodName":"run","nativeMethod":false},{"className":"java.lang.Thread","fileName":"Thread.java","lineNumber":745,"methodName":"run","nativeMethod":false}],"suppressed":[]},"resultCode":"1"}
[/code]
...全文
276 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
shirui8653719 2016-08-03
  • 打赏
  • 举报
回复
引用 1 楼 qq_15915835 的回复:
你这是调用别的的接口吧。和httpclient木有关系啊。还是查查这接口怎么写的吧
不是别人的接口,就是CloseableHttpClient里面的方法,有些命令能执行,有些命令不能执行。
qq_15915835 2016-08-03
  • 打赏
  • 举报
回复
你这是调用别的的接口吧。和httpclient木有关系啊。还是查查这接口怎么写的吧
CooyMaster 2016-08-03
  • 打赏
  • 举报
回复
问题应该出在你服务端,你在排查排出
shirui8653719 2016-08-03
  • 打赏
  • 举报
回复
引用 3 楼 asd4393950 的回复:
我这访问不到你提供的IP,故运行到httpclient.execute(post)就会报错, 运行了你提供的两条命令,在运行到httpclient.execute(post)之前 都不会报错,你可以提供一下你服务端的处理方法
嗯,确实,我在服务器端有一个代理服务器,代码比较多。
CooyMaster 2016-08-03
  • 打赏
  • 举报
回复
我这访问不到你提供的IP,故运行到httpclient.execute(post)就会报错, 运行了你提供的两条命令,在运行到httpclient.execute(post)之前 都不会报错,你可以提供一下你服务端的处理方法

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧