package handleSsh; import com.jcraft.jsch.*; import java.util.ArrayList; import java.io.File; public class DownloadFiles { private static final String LOCAL_BASIC_PATH = "/home/rasta5man/dev/oms/downloads/"; //ADD FILES TO DOWNLOAD IN MAIN METHOD private static ArrayList filesToDownload = new ArrayList<>(); public static void main(String[] args) throws JSchException { ArrayList allServers = new BuildServerArray().getAllServers(); for (Server server : allServers) { String remoteBasicPath = ""; if(server.getUsername().equals("root")) { // je to LM remoteBasicPath = "/root/flowserver/"; } else { remoteBasicPath = "/home/unipi/flowserver/"; } filesToDownload.clear(); filesToDownload.add(remoteBasicPath + "databases/nodes.table"); // filesToDownload.add(remoteBasicPath + "databases/settings.table"); System.out.println("Connecting to server: " + server); downloadFiles(server); } } /** * Establishes SFTP connection and downloads files. */ public static void downloadFiles(Server server) throws JSchException { JSch jsch = new JSch(); jsch.setKnownHosts("/home/rasta5man/.ssh/known_hosts"); jsch.addIdentity( "/home/rasta5man/.ssh/oms_key" ); Session session = null; ChannelSftp sftpChannel = null; try { session = jsch.getSession(server.getUsername(), server.getHost(), server.getPort()); session.setPassword(server.getPassword()); //session.setConfig("StrictHostKeyChecking", "no"); // Avoids checking for new host keys (not recommended for production) session.connect(); sftpChannel = (ChannelSftp) session.openChannel("sftp"); sftpChannel.connect(); for (String fileToDownload : filesToDownload) { System.out.println("Downloading " + fileToDownload + " to " + server); // 3. Perform the file download System.out.println("Attempting to download remote file: " + fileToDownload); String localFileName = getFileName(fileToDownload); File localFile = new File(LOCAL_BASIC_PATH + localFileName + server.getHost()); // JSch's get() method downloads the remote file to the local path sftpChannel.get(fileToDownload, localFile.getAbsolutePath()); System.out.println("\n✅ Success! File downloaded to: " + localFile.getAbsolutePath()); } } catch (JSchException e) { // Handle connection, authentication, or channel opening errors System.err.println("\n❌ SFTP Connection Error: " + e.getMessage()); e.printStackTrace(); } catch (SftpException e) { // Handle file-specific errors (e.g., file not found, permission denied) System.err.println("\n❌ SFTP File Transfer Error: " + e.getMessage() + " (Error code: " + e.id + ")"); System.err.println("Please check if the remote file path exists and permissions are correct."); e.printStackTrace(); } finally { // 4. Clean up resources if (sftpChannel != null && sftpChannel.isConnected()) { sftpChannel.disconnect(); System.out.println("SFTP Channel disconnected."); } if (session != null && session.isConnected()) { session.disconnect(); System.out.println("Session disconnected."); } } } /** * Extracts the filename from a given file path string. * It assumes a Unix-style path separator ('/'), which is standard for SFTP. * * @param path The full file path (e.g., "/home/george/pictures/downtown.jpg"). * @return The filename (e.g., "downtown.jpg"). Returns an empty string if the input is null or empty. */ public static String getFileName(String path) { if (path == null || path.isEmpty()) { return ""; } int lastSeparatorIndex = path.lastIndexOf('/'); // If the path contains a separator, return the substring after it. // Otherwise, the entire path is considered the filename. return (lastSeparatorIndex == -1) ? path : path.substring(lastSeparatorIndex + 1); } }