Subversion Repositories SmartDukaan

Rev

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