windows下通过java ssh连接到远程linux执行命令

  • windows下通过java ssh连接到远程linux执行命令已关闭评论
  • 180 views
  • A+
所属分类:编程开发

这几天,想对ansible的各个命令通过java封装成api接口,python固然方便,但是我不喜欢。

上篇文章已经说过了,ansible在windows下不能作为控制端,所有我只能放到linux上,这就牵扯到程序远程调用linux命令问题。如果写好的java类每次都编译放到linux上太浪费时间了。所有就本地弄了个ssh远程工具连接到linux执行命令,这样就方便多了,直接上代码吧

maven坐标如下:

<dependency>
    <groupId>ch.ethz.ganymed</groupId>
    <artifactId>ganymed-ssh2</artifactId>
    <version>262</version>
</dependency>

代码如下:

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by xubo-os on 2018/8/15.
 */
public class SshUtils {
    static final Logger LOG = LoggerFactory.getLogger(SshUtils.class);

    /**
     * @param host     主机名
     * @param username 用户
     * @param password 密码
     * @param port     ssh端口
     * @param command  linux命令
     * @return 命令执行结果
     */
    public static String exec(String host, String username, String password, int port, String command) {

        Connection connection = null;
        Session session = null;
        InputStream stdout = null;
        BufferedReader br = null;
        StringBuffer buffer = new StringBuffer();
        try {
            connection = new Connection(host, port);
            connection.connect();

            if (!connection.authenticateWithPassword(username, password)) {
                LOG.error("connect " + host + " authenicateWithPassword fail");
                return "fail";
            }
            session = connection.openSession();
            session.execCommand(command);
            stdout = new StreamGobbler(session.getStdout());
            br = new BufferedReader(new InputStreamReader(stdout));

            while (true) {
                String line = br.readLine();
                if (line == null) {
                    break;
                }
                buffer.append(line);
                buffer.append(System.getProperty("line.separator"));// 换行
            }

        } catch (Exception e) {
            LOG.error("connection host " + host + " cmd " + command + " exception: ", e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    LOG.error("close br fail", e);
                }
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        return buffer.toString();
    }
 public static void main(String args[]) {
String exec = SshUtils.exec("172.21.201.69", "root", "123456", 22, "ansible -i /etc/ansible/hosts webservers -m raw -a 'hostname|tee'");
System.out.println(exec);
  }
}

返回结果:

172.21.201.71 | SUCCESS | rc=0 >>
19
172.21.201.70 | SUCCESS | rc=0 >>
19

后期就准备当测试工具用了。

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar