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);
}
}
}
- 调用封装好的工具类文件
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);
}