Subversion Repositories SmartDukaan

Rev

Rev 32408 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 32408 Rev 33973
Line 1... Line 1...
1
package com.spice.profitmandi.service.SshServer;
1
package com.spice.profitmandi.service.SshServer;
2
 
2
 
3
import com.jcraft.jsch.ChannelExec;
3
import com.jcraft.jsch.*;
4
import com.jcraft.jsch.JSch;
4
import org.apache.logging.log4j.LogManager;
5
import com.jcraft.jsch.Session;
5
import org.apache.logging.log4j.Logger;
-
 
6
import org.springframework.beans.factory.annotation.Value;
6
import org.springframework.stereotype.Service;
7
import org.springframework.stereotype.Component;
7
 
8
 
-
 
9
import java.util.*;
-
 
10
 
8
@Service
11
@Component
9
public class SSHServiceImpl implements SSHService {
12
public class SSHServiceImpl implements SSHService {
-
 
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
 
10
    @Override
25
    @Override
11
    public void executeCommand(String ipAddress, int port, String username, String password, String commandToExec) {
26
    public void executeCommand(String ipAddress, int port, String username, String password, String commandToExec) {
12
        try {
27
        try {
13
            JSch jsch = new JSch();
28
            JSch jsch = new JSch();
14
            Session session = jsch.getSession(username, ipAddress, port);
29
            Session session = jsch.getSession(username, ipAddress, port);
Line 27... Line 42...
27
            session.disconnect();
42
            session.disconnect();
28
        } catch (Exception e) {
43
        } catch (Exception e) {
29
            e.printStackTrace();
44
            e.printStackTrace();
30
        }
45
        }
31
    }
46
    }
-
 
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
 
32
}
106
}