Subversion Repositories SmartDukaan

Rev

Rev 32408 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service.SshServer;

import com.jcraft.jsch.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class SSHServiceImpl implements SSHService {

    @Value("172.105.58.16")
    private String host;

    @Value("root")
    private String user;

    @Value("22")
    private int port;

    private static final Logger LOGGER = LogManager.getLogger(SSHService.class);

    @Override
    public void executeCommand(String ipAddress, int port, String username, String password, String commandToExec) {
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, ipAddress, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            // Restart server
            ChannelExec restartChannel = (ChannelExec) session.openChannel("exec");
            restartChannel.setCommand(commandToExec);
            //Reboot server
            restartChannel.connect();
            while (!restartChannel.isClosed()) {
                Thread.sleep(1000);
            }
            restartChannel.disconnect();
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private ChannelSftp setupJSch() throws JSchException {
        JSch jsch = new JSch();
        Session JSchSession = jsch.getSession(user, host, port);
        jsch.addIdentity(getClass().getClassLoader().getResource("id_rsa").getPath());
        JSchSession.setConfig("StrictHostKeyChecking", "no");
        JSchSession.connect();
        return (ChannelSftp) JSchSession.openChannel("sftp");
    }

    @Override
    public Map<String, List<String>> readDirectory(String directoryPath) {
        Map<String, List<String>> directoryFileMap = new HashMap<>();
        ChannelSftp channelSftp = null;
        try {
            channelSftp = setupJSch();
            channelSftp.connect();

            readDirectoryRecursively(channelSftp, directoryPath, directoryFileMap);

        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        } finally {
            if (channelSftp != null && channelSftp.isConnected()) {
                channelSftp.disconnect();
            }
        }

        return directoryFileMap;
    }

    private void readDirectoryRecursively(ChannelSftp channelSftp, String directoryPath, Map<String, List<String>> directoryFileMap) throws SftpException {
        channelSftp.cd(directoryPath);

        Vector<ChannelSftp.LsEntry> fileList = channelSftp.ls(directoryPath);
        List<String> fileNames = new ArrayList<>();
        String dirName = directoryPath.substring(directoryPath.lastIndexOf('/') + 1);

        for (ChannelSftp.LsEntry entry : fileList) {
            String fileName = entry.getFilename();

            if (fileName.equals(".") || fileName.equals("..")) {
                continue;
            }

            if (entry.getAttrs().isDir()) {
                readDirectoryRecursively(channelSftp, directoryPath + "/" + fileName, directoryFileMap);
            } else {
                if (fileName.toLowerCase().endsWith(".xml")) {
                    fileNames.add(fileName);
                }
            }
        }

        if (!fileNames.isEmpty()) {
            directoryFileMap.put(dirName, fileNames);
        }
    }

}