Java操作远程服务器并执行命令并返回数据、Java操作本地服务器执行命令并返回数据

添加依赖的jar包 

<!-- jsch 所依赖的jar包  -->
<dependency>
    <groupId>com.jcraft</groupId>
    <artifactId>jsch</artifactId>
    <version>0.1.51</version>
</dependency>
/**
     * 连接到指定的HOST
     * @param username
     * @param ip
     * @param password
     * @return
     */
    private static boolean connect(String username, String ip, String password) {
        JSch jsch = new JSch();
        try {
            session = jsch.getSession(username, ip, 22);
            session.setPassword(password);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
        } catch (JSchException e) {
            e.printStackTrace();
            log.info("connect fail !");
            return false;
        }
        return true;
    }
/**
     * 远程连接Linux服务器并执行相关的命令
     *
     * @param username
     * @param ip
     * @param password
     * @param command
     * @return 最终命令返回信息
     * @throws Exception
     */
    public static String runRemoteShell(String username, String ip, String password, String command) throws Exception {
        if (session == null) {
            if (!connect(username, ip, password)) {
                return null;
            }
        }
        BufferedReader reader = null;
        Channel channel = null;
        String str = "";
        try {
            channel = session.openChannel("exec");
            ((ChannelExec)channel).setCommand(command);

            channel.setInputStream(null);
            ((ChannelExec)channel).setErrStream(System.err);

            channel.connect();
            InputStream in = channel.getInputStream();
            reader = new BufferedReader(new InputStreamReader(in));
            String buf;
            while ((buf = reader.readLine()) != null) {
                if (buf.contains("PID")) {
                    break;
                }
                System.out.println(buf);
                str += buf.trim() + "#";
            }
        } catch (IOException | JSchException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (channel != null) {
                    channel.disconnect();
                }
                session.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }
/**
     * 在本地服务器执行相关shell命令
     *
     * @return str 命令返回结信息
     * @throws Throwable
     */
    public StringBuffer runLocalShell() throws Throwable {
        String commend = "cmd.exe /c ";
        StringBuffer str = new StringBuffer();
        try {
            Runtime rt = Runtime.getRuntime();
            log.info("转码信息为:{}", commend);
            Process proc = rt.exec(commend);
            InputStream stderr = proc.getErrorStream();
            InputStreamReader isr = new InputStreamReader(stderr);
            BufferedReader br = new BufferedReader(isr);
            String line = null;
            while ((line = br.readLine()) != null) {
                if (line.contains("PID")) {
                    break;
                }
                System.out.println(line);
                str.append(line.trim()).append("#");
            }
            proc.waitFor();
            stderr.close();
            isr.close();
            br.close();
            proc.destroyForcibly();
        } catch (Throwable t) {
            t.printStackTrace();
            log.info(t.getMessage());
            throw t;
        }
        return str;
    }
 
发布了35 篇原创文章 · 获赞 0 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/tealala/article/details/103408434
今日推荐