package com.silverbox.shopper;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SFTPv3Client;
import ch.ethz.ssh2.SFTPv3DirectoryEntry;
public class TestScp {
//https://blog.csdn.net/wyc_cs/article/details/51749938
private static String IP = "172.16.8.6";
private static int PORT = 22;
private static String USER = "root";//登录用户名
private static String PASSWORD = "root";//生成私钥的密码和登录密码,这两个共用这个密码
private static Connection connection = new Connection(IP, PORT);
private static String PRIVATEKEY = "C:\Users\ubuntu\.ssh\id_rsa";// 本机的私钥文件
private static boolean usePassword = true;// 使用用户名和密码来进行登录验证。如果为true则通过用户名和密码登录,false则使用rsa免密码登录
/**
* ssh用户登录验证,使用用户名和密码来认证
*
* @param user
* @param password
* @return
*/
public static boolean isAuthedWithPassword(String user, String password) {
try {
return connection.authenticateWithPassword(user, password);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
/**
* ssh用户登录验证,使用用户名、私钥、密码来认证 其中密码如果没有可以为null,生成私钥的时候如果没有输入密码,则密码参数为null
*
* @param user
* @param privateKey
* @param password
* @return
*/
public static boolean isAuthedWithPublicKey(String user, File privateKey, String password) {
try {
return connection.authenticateWithPublicKey(user, privateKey, password);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public static boolean isAuth() {
if (usePassword) {
return isAuthedWithPassword(USER, PASSWORD);
} else {
return isAuthedWithPublicKey(USER, new File(PRIVATEKEY), PASSWORD);
}
}
/**
* 下载文件
* @param remoteFile
* @param path
*/
public static void getFile(String remoteFile, String path) {
try {
connection.connect();
boolean isAuthed = isAuth();
if (isAuthed) {
System.out.println("认证成功!");
SCPClient scpClient = connection.createSCPClient();
scpClient.get(remoteFile, path);
} else {
System.out.println("认证失败!");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
connection.close();
}
}
public static void putFile(String localFile, String remoteTargetDirectory) {
try {
connection.connect();
boolean isAuthed = isAuth();
if (isAuthed) {
SCPClient scpClient = connection.createSCPClient();
scpClient.put(localFile, remoteTargetDirectory);
} else {
System.out.println("认证失败!");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
connection.close();
}
}
public static ArrayList<String> readFile(String directory) {
//创建远程连接,默认连接端口为22,如果不使用默认,可以使用方法
//new Connection(ip, port)创建对象
// String ip = "10.0.8.1";
// String usr = "root";
// String pwd = "root";
//int port=22;
Connection conn = null;
String date = "";
//所要查询的目录
String path = directory;
ArrayList<String> filesNameList = new ArrayList<String>();
try {
//连接远程服务器
// 连接部署服务器
conn = new Connection(IP);
conn.connect();
//使用用户名和密码登录
boolean b = conn.authenticateWithPassword(USER, PASSWORD);
if (!b) {
throw new IOException("Authentication failed.");
} else {
SFTPv3Client sft = new SFTPv3Client(conn);
Vector<?> v = sft.ls(path);
for (int i = 0; i < v.size(); i++) {
SFTPv3DirectoryEntry s = new SFTPv3DirectoryEntry();
s = (SFTPv3DirectoryEntry) v.get(i);
//文件名
String filename = s.filename;
filesNameList.add(filename);
}
}
conn.close();
} catch (IOException e) {
System.err.printf("用户%s密码%s登录服务器%s失败!", USER, PASSWORD, IP);
e.printStackTrace();
}
return filesNameList;
}
public static void main(String[] args) {
try {
String dir= "/home/silverbox/gitlab/data/backups/";
//获取目录下的所有的文件列表
ArrayList<String> list = readFile(dir);
//1588779370_2020_05_06_10.6.4_gitlab_backup.tar
Date date = new Date(); //获取当前的系统时间。
//日期减一天,生成日期串
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd") ; //使用了默认的格式创建了一个日期格式化对象。
String time = dateFormat.format(cal.getTime()); //可以把日期转换转指定格式的字符串
System.out.println("当前的系统时间:"+ time);
if(list!=null&&list.size()>0) {
for(String s:list) {
//如果这个文件名包含昨天的日期,则下载这个备份文件
if(s.indexOf(time)>0&&s.endsWith(".tar")) {
System.out.println("正在下载备份........................................");
//下载文件
getFile(dir+s, "d://linux-test//");
System.out.println("下载完成........................................");
}
}
}
//下载文件测试
getFile("/home/silverbox/logs/discovery/euraka_info-2020-03-31.log", "d://linux-test//");
//putFile("c://aaa.txt", "/home/users/ubuntu");
} catch (Exception e) {
e.printStackTrace();
}
}
}
环境pom:
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>
网友评论