Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
32336 jai.hind 1
package com.spice.profitmandi.service.SshServer;
32408 amit.gupta 2
 
33973 tejus.loha 3
import com.jcraft.jsch.*;
4
import org.apache.logging.log4j.LogManager;
5
import org.apache.logging.log4j.Logger;
6
import org.springframework.beans.factory.annotation.Value;
7
import org.springframework.stereotype.Component;
32408 amit.gupta 8
 
33973 tejus.loha 9
import java.util.*;
10
 
11
@Component
32336 jai.hind 12
public class SSHServiceImpl implements SSHService {
33973 tejus.loha 13
 
14
    @Value("172.105.58.16")
15
    private String host;
16
 
17
    @Value("root")
18
    private String user;
19
 
20
    @Value("22")
21
    private int port;
22
 
23
    private static final Logger LOGGER = LogManager.getLogger(SSHService.class);
24
 
32408 amit.gupta 25
    @Override
26
    public void executeCommand(String ipAddress, int port, String username, String password, String commandToExec) {
27
        try {
28
            JSch jsch = new JSch();
29
            Session session = jsch.getSession(username, ipAddress, port);
30
            session.setPassword(password);
31
            session.setConfig("StrictHostKeyChecking", "no");
32
            session.connect();
33
            // Restart server
34
            ChannelExec restartChannel = (ChannelExec) session.openChannel("exec");
35
            restartChannel.setCommand(commandToExec);
36
            //Reboot server
37
            restartChannel.connect();
38
            while (!restartChannel.isClosed()) {
39
                Thread.sleep(1000);
32336 jai.hind 40
            }
32408 amit.gupta 41
            restartChannel.disconnect();
42
            session.disconnect();
43
        } catch (Exception e) {
44
            e.printStackTrace();
32337 jai.hind 45
        }
32408 amit.gupta 46
    }
33973 tejus.loha 47
 
48
    private ChannelSftp setupJSch() throws JSchException {
49
        JSch jsch = new JSch();
50
        Session JSchSession = jsch.getSession(user, host, port);
51
        jsch.addIdentity(getClass().getClassLoader().getResource("id_rsa").getPath());
52
        JSchSession.setConfig("StrictHostKeyChecking", "no");
53
        JSchSession.connect();
54
        return (ChannelSftp) JSchSession.openChannel("sftp");
55
    }
56
 
57
    @Override
58
    public Map<String, List<String>> readDirectory(String directoryPath) {
59
        Map<String, List<String>> directoryFileMap = new HashMap<>();
60
        ChannelSftp channelSftp = null;
61
        try {
62
            channelSftp = setupJSch();
63
            channelSftp.connect();
64
 
65
            readDirectoryRecursively(channelSftp, directoryPath, directoryFileMap);
66
 
67
        } catch (JSchException | SftpException e) {
68
            e.printStackTrace();
69
        } finally {
70
            if (channelSftp != null && channelSftp.isConnected()) {
71
                channelSftp.disconnect();
72
            }
73
        }
74
 
75
        return directoryFileMap;
76
    }
77
 
78
    private void readDirectoryRecursively(ChannelSftp channelSftp, String directoryPath, Map<String, List<String>> directoryFileMap) throws SftpException {
79
        channelSftp.cd(directoryPath);
80
 
81
        Vector<ChannelSftp.LsEntry> fileList = channelSftp.ls(directoryPath);
82
        List<String> fileNames = new ArrayList<>();
83
        String dirName = directoryPath.substring(directoryPath.lastIndexOf('/') + 1);
84
 
85
        for (ChannelSftp.LsEntry entry : fileList) {
86
            String fileName = entry.getFilename();
87
 
88
            if (fileName.equals(".") || fileName.equals("..")) {
89
                continue;
90
            }
91
 
92
            if (entry.getAttrs().isDir()) {
93
                readDirectoryRecursively(channelSftp, directoryPath + "/" + fileName, directoryFileMap);
94
            } else {
95
                if (fileName.toLowerCase().endsWith(".xml")) {
96
                    fileNames.add(fileName);
97
                }
98
            }
99
        }
100
 
101
        if (!fileNames.isEmpty()) {
102
            directoryFileMap.put(dirName, fileNames);
103
        }
104
    }
105
 
32408 amit.gupta 106
}