diff --git a/DownloadFiles.java b/DownloadFiles.java new file mode 100644 index 0000000..87aaf15 --- /dev/null +++ b/DownloadFiles.java @@ -0,0 +1,125 @@ +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); + } + +} + + + + + diff --git a/README b/README index 3d435c0..bd2f7b5 100644 --- a/README +++ b/README @@ -1,6 +1,7 @@ POUZITIE: Pocitame iba s dvomi pripadmi: bud je zariadenie LM alebo unipi, to znamena, bud je user "root" alebo "unipi". +Dalsi predpoklad spravneho fungovania je, ze uzivatel, ktory sa pripaja na unipi zariadenie tam ma svoj ssh kluc uz nahraty. 1. v "buildServerArray.java" si doplnte zariadenia, na ktore sa chcete cez ssh prihlasit: "server.put(ip, username)" dole v subore takisto doplnte prihlasovacie hesla pre jednotlive zariadenia: "rvoPassword.put(ip, heslo)" @@ -9,6 +10,8 @@ tak isto nezabudnite nastavit cas medzi jednotlivymi pripajaniami na zariadenia 3. ked chcete hromadny upload suborov, pouzite subor "UploadFiles.java". V prvom rade nastavte "BASIC_PATH" - adresar v ktorom su subory na upload. DOLEZITE: adresar v ktorom su subory sa musi volat na konci "flowserver" a koncit lomitkom. (moze mat predtym text, ale koniec musi byt flowserver - napr "city-flowserver/" Ak chcete menit ine subory na unipi alebo lm, ako flowserver, musite nastavit "directoryToUpload" +4. ked chcete hromadny download suborov, pouzite subor "DownloadFiles.java". Nastavte "LOCAL_BASIC_PATH" - adresar, kde sa subory stiahnu. +Dalej v metode "main" pridajte subory, ktore chcete stiahnut. Treba zadat cestu k suboru v "FLOWSERVER" adresari. 5.5.2025 ->