Subversion Repositories SmartDukaan

Rev

Rev 32337 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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