java 触发鼠标点击事件 向linux发送指令

package click;

import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class click {

    public static void main(String[] args) throws Exception {
        
        //获取鼠标位置
//        for(int i=0;i<10;i++){
//        Point point = MouseInfo.getPointerInfo().getLocation();
//        System.out.println("x=" + point.getX() + ",y="+ point.getY());
//        Thread.sleep(3000);
//        }
        Robot robot = null;
         
            robot = new Robot();
         
        int x=298;
        int y = 133;
         for(int i=0;i<500;i++)
        {
             if(i%20 ==0){
             String cmdStop = "service activator stop";
             String cmdStart = "service activator start";
                new click().sendCommand(cmdStop);
                Thread.sleep(180000);
                new click().sendCommand(cmdStart);
                Thread.sleep(180000);
                 Thread.sleep(3000);
                int jobx = 1333;
                int joby = 634;
                robot.mouseMove(jobx,joby);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                //模拟鼠标松开左键
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
                robot.keyPress(KeyEvent.VK_BACK_SPACE);
                robot.keyRelease(KeyEvent.VK_BACK_SPACE);
                 robot.keyPress(KeyEvent.VK_BACK_SPACE);
                 robot.keyRelease(KeyEvent.VK_BACK_SPACE);
             System.out.println("----------------");
             }
             
        robot.mouseMove(x, y);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        //模拟鼠标松开左键
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
         
        Thread.sleep(120000);
        System.out.println("----"+i);
        }
    }

    public void sendCommand(String cmd) throws Exception {
        System.out.println(cmd);
        String userName="root";
        String pass="NFVD1234";
        String ip="16.17.101.33";
        JSch sshSingleton = new JSch();
        Session session = sshSingleton.getSession(userName, ip);
        session.setPassword(pass);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        ChannelExec channel = (ChannelExec) session.openChannel("exec");
        BufferedReader in = new BufferedReader(new InputStreamReader(
                            channel.getInputStream()));
                    channel.setCommand(cmd);
                    channel.connect();
        //读取命令输出信息
                    String msg;
                    while ((msg = in.readLine()) != null) {
                        System.out.println(msg);
                    }

                    channel.disconnect();
                    session.disconnect();
    }
}

 

posted on 2017-11-29 16:15  gaizhongfeng  阅读(1705)  评论(0编辑  收藏  举报