Subversion Repositories SmartDukaan

Rev

Rev 32336 | Rev 32408 | Go to most recent revision | 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;
2
import com.jcraft.jsch.ChannelExec;
3
import com.jcraft.jsch.JSch;
4
import com.jcraft.jsch.Session;
5
import org.springframework.stereotype.Service;
6
@Service
7
public class SSHServiceImpl implements SSHService {
8
            @Override
32337 jai.hind 9
            public void RestartServer(String ipAddress, int port, String username, String password, String remoteDirectoryPath, String command,String rebootCommand) {
32336 jai.hind 10
                try {
11
                    JSch jsch = new JSch();
12
                    Session session = jsch.getSession(username, ipAddress, port);
13
                    session.setPassword(password);
14
                    session.setConfig("StrictHostKeyChecking", "no");
15
                    session.connect();
16
                    // Restart server
17
                    ChannelExec restartChannel = (ChannelExec) session.openChannel("exec");
18
                    restartChannel.setCommand(command);
32337 jai.hind 19
                    //Reboot server
20
                    restartChannel.setCommand(rebootCommand);
32336 jai.hind 21
                    restartChannel.connect();
22
                    while (!restartChannel.isClosed()) {
23
                        Thread.sleep(1000);
24
                    }
25
                    restartChannel.disconnect();
26
                    session.disconnect();
27
                } catch (Exception e) {
28
                    e.printStackTrace();
29
                }
30
            }
32337 jai.hind 31
        }