JAVA通过SSH连接服务器
创始人
2024-11-29 19:05:59

JAVA通过SSH连接服务器

简介

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

功能

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

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

                     ch.ethz.ganymed             ganymed-ssh2             build210          

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链接

相关内容

热门资讯

四部门印发行动方案,促进人工智... 近日,国家能源局会同国家发展改革委、工业和信息化部、国家数据局印发《关于促进人工智能与能源双向赋能的...
瞄准8时13分!一起看发射场准... 我国将于5月11日8时13分发射天舟十号货运飞船。目前,长征七号遥十一运载火箭已完成推进剂加注。 距...
天舟十号货运飞船点火发射 今天上午,搭载天舟十号货运飞船的长征七号遥十一运载火箭,在我国文昌航天发射场点火发射。
原创 微... 前段时间 iOS 微信发布了 8.0.73 正式版,安卓微信发布了 8.0.72 测试版,这次的更新...
任正非罕见出镜:华为芯片基础技... 感谢IT之家网友 的线索投递! 5 月 10 日消息,在 5 月 8 日播出的《新闻联播》节目中,...