parent
1565ee2986
commit
d6bdd31e74
@ -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…
Reference in new issue