telnet、curl命令的使用
1.封装工具类
package com.redxun.system.util; import org.apache.commons.net.telnet.TelnetClient; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; /** * @author huangquanguang * @date 2023/3/22 8:57 * @description telnet、curl工具类 */ public class TelnetCurlUtil { private static TelnetClient telnetClient = null; private static InputStream is = null; private static OutputStream os = null; /** * 连接远程计算机,连接完成后,获取读取流与发送流 * * @param ip 远程计算机IP地址 * @param port 远程计算机端口 */ public static void connection(String ip, int port) { try { telnetClient = new TelnetClient(); telnetClient.connect(ip, port); is = telnetClient.getInputStream(); os = telnetClient.getOutputStream(); } catch (Exception e) { e.printStackTrace(); } } /** * 获取远程计算机返回的信息 */ public String readTelnetMsg() { try { int len = 0; byte[] b = new byte[1024]; do { len = is.read(b); if (len >= 0) return new String(b, 0, len); } while (len >= 0); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 向远端计算机发送指令消息 * * @param msg 需要传送的指令 **/ public void sendTelnetMsg(String msg) { byte[] b = msg.getBytes(); try { os.write(b, 0, b.length); os.flush(); } catch (IOException e) { e.printStackTrace(); } } /** * 查找远端计算机返回的指令中是否包含想要指令 * 一直查找,直到包含,返回true */ public boolean findStr(String str) { for (; ; ) { String msg = readTelnetMsg(); if (msg == null) return false; if (msg.indexOf(str) != -1) return true; } } /** * 关闭连接,关闭IO */ public void close() { try { is.close(); os.close(); telnetClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } /** * java 调用 Curl的方法 * * @param cmds * @return */ public static String execCurl(String[] cmds) { ProcessBuilder process = new ProcessBuilder(cmds); Process p; try { p = process.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line); builder.append(System.getProperty("line.separator")); } return builder.toString(); } catch (IOException e) { System.out.print("获取线程异常" + e); } return null; } /** * curl方法 * url:请求地址 * methodType:请求类型 * 只返回状态码 */ public static String execCurl(String url,String methodType){ String[] cmds = {"curl", "-H", "Content-Type:application/json","-sL","-w", "%{http_code}","-o","/dev/null", "-X",""+ methodType +"" ,"-k", ""+url+""}; StringBuffer sb = new StringBuffer(); for(int i=0;i<cmds.length;i++){ sb.append(cmds[i]+" "); } System.out.println(sb);//这个是测试中调试输出你的发送命令航 String responseMsg=execCurl(cmds); System.out.println(responseMsg); return responseMsg; } /** * curl get示例 */ // public static void getToken(){ // String url = "https://www.baidu.com/"; //这个为url连接,随便写了个连接,一般接口会先调用接口获取token,然后拼接token,post发送请求 // String[] cmds = {"curl","X","GET" ,"-k", "-i",""+url+""}; // StringBuffer sb = new StringBuffer(); // for(int i=0;i<cmds.length;i++){ // sb.append(cmds[i]+" "); // } // System.out.println(sb); // String responseMsg=execCurl(cmds); // System.out.println(responseMsg); // } /** * curl post示例 */ // public static void getMsg(){ // String jsonStr= "{'content':'testtest','creator':'creator''name':'lili'}"; //这里为发送的json串 // String url = "http://192.168.2.120:7200/restApi/form/getData"; //这里为连接发送的url // String accessToken = "123456"; //这一步这个token为上面get获取到的,有些简单的接口不需要获取这个 // String[] cmds = {"curl","-H","access-token:{{"+accessToken+"}}" , // "-H", "Content-Type:application/json", // "-X","POST" ,"-k", "-i",""+url+"", // "-d",""+jsonStr+""}; //超级注意:这个拼接json串的时候不要拼接双引号或者单引号,这个是踩坑提示,因为这个改了好久 // StringBuffer sb = new StringBuffer(); // for(int i=0;i<cmds.length;i++){ // sb.append(cmds[i]+" "); // } // System.out.println(sb);//这个是测试中调试输出你的发送命令航 // System.out.println(execCurl(cmds)); // } public static void main(String[] args) { execCurl("http://192.168.2.120:7200/form/core/formCustomQuery/queryForJson_ywgxtz","POST"); execCurl("http://192.168.2.120:7200/restApi/form/getData","GET"); execCurl("http://192.168.2.120:7200/restApi/form/queryForJson_project_can_review","POST"); } }2.curl参数解释
"-H", "Content-Type:application/json", 设置请求头类型"-sL","-w", "%{http_code}","-o","/dev/null", 只返回状态码"-X",""+ methodType +"" , 指定请求类型,POST、GET"-k" 发送https请求加上这个参数,否则会有证书报错参考文档:https://blog.csdn.net/heihaozi/article/details/122609296
3.检测接口连通情况
1.获取所有开启服务发现的接口 2.telnet这些接口的项目路径 3.telnet通的话,curl接口地址,返回非空且不是500以上则修改连通状态为1,返回空且为500以上修改为0 4.telnet不通的直接修改连通状态为0,发送预警日志等