Subversion Repositories SmartDukaan

Rev

Rev 32337 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service.SshServer;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.springframework.stereotype.Service;

@Service
public class SSHServiceImpl implements SSHService {
    @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();
        }
    }
}