JAVA通过SSH连接服务器

JAVA通过SSH连接服务器

简介

通过java代码连接服务器,并执行相应的命令

功能

Java可以通过SSH连接服务器,并执行服务器上的命令。使用SSH可以远程管理服务器,执行命令、上传下载文件等操作。

###如何使用
要在java中使用ssh连接服务器,可以使用一些ssh库,我这里使用的是ethz.ssh2和io去连接服务器。
以下是代码示例:
1.导入pom文件

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

2.编写代码,连接服务器,执行命令,获取结果,关闭连接 这些操作我都封装成一个工具类文件,服务器的连接信息和要执行的命令设为变量,当然也可以替换成固定的值

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * ssh连接
 * @author 小南蜀黍
 */
public class SSHConnection {
    /**
     * 执行并输出内容
     * @param ip IP地址
     * @param username 用户名
     * @param password 密码
     * @param command 命令
     */
    public static void SSH2Connection(String ip ,String username,String password,String command){
        try {
            Connection conn = new Connection(ip);
            conn.connect();
            //进行身份认证
            boolean isAuthenticated = conn.authenticateWithPassword(
                    username,password);
            if (isAuthenticated == false){
                throw new IOException("Authentication failed.");
            }
            //开启一个Session
            Session sess = conn.openSession();
            //执行命令
            sess.execCommand(command);
            //获取返回输出
            InputStream stdout = new StreamGobbler(sess.getStdout());
            //返回错误输出
            InputStream stderr = new StreamGobbler(sess.getStderr());
            BufferedReader stdoutReader = new BufferedReader(
                    new InputStreamReader(stdout));
            BufferedReader stderrReader = new BufferedReader(
                    new InputStreamReader(stderr));
            System.out.println("Here is the output from stdout:");
            while (true) {
                String line = stdoutReader.readLine();
                if (line == null){
                    break;
                }
                System.out.println(line);
            }
            System.out.println("Here is the output from stderr:");
            while (true) {
                String line = stderrReader.readLine();
                if (line == null){
                    break;
                }
                System.out.println(line);
            }
            //关闭Session
            sess.close();
            //关闭Connection
            conn.close();
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(2);
        }
    }
    /**
     * 执行不输出内容
     * @param ip IP地址
     * @param username 用户名
     * @param password 密码
     * @param command 命令
     */
    public static void SSHConnection(String ip ,String username,String password,String command){
        try {
            Connection conn = new Connection(ip);
            conn.connect();
            //进行身份认证
            boolean isAuthenticated = conn.authenticateWithPassword(
                    username,password);
            if (isAuthenticated == false){
                throw new IOException("Authentication failed.");
            }
            //开启一个Session
            Session sess = conn.openSession();
            //执行命令
            sess.execCommand(command);
            //获取返回输出
            InputStream stdout = new StreamGobbler(sess.getStdout());
            //返回错误输出
            InputStream stderr = new StreamGobbler(sess.getStderr());
            BufferedReader stdoutReader = new BufferedReader(
                    new InputStreamReader(stdout));
            BufferedReader stderrReader = new BufferedReader(
                    new InputStreamReader(stderr));
            System.out.println("Here is the output from stdout:");
            while (true) {
                String line = stdoutReader.readLine();
                if (line == null){
                    break;
                }
                System.out.println(line);
            }
            System.out.println("Here is the output from stderr:");
            while (true) {
                String line = stderrReader.readLine();
                if (line == null){
                    break;
                }
                System.out.println(line);
            }
            //关闭Session
            sess.close();
            //关闭Connection
            conn.close();
        } catch (IOException e) {
            e.printStackTrace(System.err);
            System.exit(2);
        }
    }
 }
  1. 调用封装好的工具类文件
 public static void main(String[] args) {
        String ip ="localhost";
        String username ="root";
        String password ="root";
        // 以docker部署的mysql进行重启为例  当然也可以替换成其他的命令
        String command ="docker restart mysql";
        SSHConnection.SSHConnection(ip,username,password,command);
    }

公众号

代码开发文章分享

原文链接

csdn链接

  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值