wangbing 4 years ago
parent 1565ee2986
commit d6bdd31e74

@ -128,6 +128,12 @@
<version>12.1.0.2.0</version>
</dependency>
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>

@ -0,0 +1,181 @@
package xyz.wbsite.dbtool.web.frame.ssh;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
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;
public class SshClient {
private String ip;
private int port;
private String username;
private String password;
private String CHAR_SET = "UTF-8";
private Connection connection;
public SshClient(String ip, int port, String username, String password) {
this.ip = ip;
this.port = port;
this.username = username;
this.password = password;
}
public SshClient(String ip, String username, String password) {
this(ip, 22, username, password);
}
private boolean login() {
try {
connection = new Connection(ip, port);
connection.connect();// 连接
boolean flag = connection.authenticateWithPassword(username, password);// 认证
if (flag) {
System.out.println(String.format("[%s:%d]登录成功.", ip, port));
}
return flag;
} catch (IOException e) {
System.err.println(String.format("[%s:%d]登录失败!", ip, port));
connection.close();
return false;
}
}
public Connection getConnect() {
if (connection == null) {
this.login();
}
return connection;
}
private boolean exeCmd(Connection connection, String cmd) {
try {
Session session = connection.openSession();// 打开一个会话
session.execCommand(cmd);// 执行命令
String result = processStdout(session.getStdout(), CHAR_SET);
System.out.println(result);
connection.close();
session.close();
return true;
} catch (IOException e) {
System.out.println("执行命令失败,链接conn:" + connection + ",执行的命令:" + cmd + " " + e);
e.printStackTrace();
return false;
}
}
/**
*
*
* @param in
* @param charset
* @return
*/
private static String processStdout(InputStream in, String charset) {
InputStream stdout = new StreamGobbler(in);
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line + "\n");
System.out.println(line);
}
br.close();
} catch (Exception e) {
System.err.println("解析脚本出错:" + e.getMessage());
e.printStackTrace();
}
return buffer.toString();
}
public boolean download(String remoteFile, String localFile) {
try {
Connection connection = getConnect();
SCPClient scpClient = connection.createSCPClient();
scpClient.get(remoteFile, localFile);
return true;
} catch (IOException ioe) {
ioe.printStackTrace();
return false;
}
}
/**
*
*
* @param localFiles
* @param remoteDir
* @return
*/
public boolean put(String localFiles, String remoteDir) {
return put(new String[]{localFiles}, remoteDir);
}
/**
*
*
* @param localFiles
* @param remoteDir
* @return
*/
public boolean put(String[] localFiles, String remoteDir) {
try {
Connection connection = getConnect();
SCPClient scpClient = connection.createSCPClient();
scpClient.put(localFiles, remoteDir);
return true;
} catch (IOException e) {
System.err.println("创建 SCPClient 错误");
e.printStackTrace();
return false;
}
}
/**
*
*
* @param localFile
* @param remoteDir
* @param rename
* @return
*/
public boolean putAndRename(String localFile, String remoteDir, String rename) {
try {
Connection connection = getConnect();
SCPClient scpClient = connection.createSCPClient();
scpClient.put(
localFile,
rename,
remoteDir,
"0600");
return true;
} catch (IOException e) {
System.err.println("创建 SCPClient 错误");
e.printStackTrace();
return false;
}
}
public boolean close() {
if (connection != null) {
connection.close();
}
return true;
}
public static void main(String[] args) {
String ip = "47.92.226.250";
String username = "root";
String password = "Wb295490665";
String cmd = "uname -a";
SshClient sshClient = new SshClient(ip, 8084, username, password);
sshClient.put("E:\\2.jpg", "/root");
sshClient.close();
}
}
Loading…
Cancel
Save

Powered by TurnKey Linux.