package com.simoncat.net;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.sshtools.j2ssh.SshClient;
import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
import com.sshtools.j2ssh.io.UnsignedInteger32;
import com.sshtools.j2ssh.session.SessionChannelClient;
import com.sshtools.j2ssh.sftp.FileAttributes;
import com.sshtools.j2ssh.sftp.SftpFile;
import com.sshtools.j2ssh.sftp.SftpFileOutputStream;
import com.sshtools.j2ssh.SftpClient;
import java.io.*;
import com.sshtools.j2ssh.configuration.ConfigurationLoader;
import com.sshtools.j2ssh.configuration.SshConnectionProperties;
import com.sshtools.j2ssh.transport.IgnoreHostKeyVerification;
import com.simoncat.vo.Server;
import com.sshtools.j2ssh.io.IOStreamConnector;
import com.sshtools.j2ssh.connection.ChannelState;
public class RestartServer {
private String server_;
private String user;
private String password;
private int portSSH;
private SshClient ssh;
private SftpClient sftp;
private Server server;
private boolean resultCommand;
private String outputCommand;
public RestartServer(Server server){
this.server=server;
server_=server.getAddress();
portSSH= new Integer(server.getPortSSH()).intValue();
user=server.getUser();
password=server.getPassword();
}
public RestartServer(String server,int portSSH, String usr,String pwd){
this.server_=server;
this.user=usr;
password=pwd;
this.portSSH=portSSH;
}
public boolean restart(){
// Make a client connection
//System.out.println("Con1");
ssh = new SshClient();
//System.out.println("Con2");
try{
// Connect to the host
if(portSSH==0)
ssh.connect(server_,new IgnoreHostKeyVerification());
else
ssh.connect(server_,portSSH,new IgnoreHostKeyVerification());
//System.out.println("Con3");
// Create a password authentication instance
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
//System.out.println("Con4");
pwd.setUsername(user);
pwd.setPassword(password);
//System.out.println("Con5");
// Try the authentication
int result = ssh.authenticate(pwd);
//System.out.println("Con6 result:"+result);
// Evaluate the result
if (result == AuthenticationProtocolState.COMPLETE) {
//REINICIAR EL SERVIDOR
//HAY VERIFICAR EL REINICIO ??
//return sendRestart("reboot now");
outputCommand = execCmd("reboot now");
return resultCommand;
}
else{
//System.out.println("Con9");
return false;
}
}
catch(Exception e){
e.printStackTrace();
//System.out.println("Con10");
return false;
}
}
public String execCmd(String cmd){
String theOutput = "";
try
{
// The connection is authenticated we can now do some real work!
SessionChannelClient session = ssh.openSessionChannel();
if ( session.executeCommand(cmd)){
IOStreamConnector output = new IOStreamConnector();
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
output.connect(session.getInputStream(), bos );
session.getState().waitForState(ChannelState.CHANNEL_CLOSED);
theOutput = bos.toString();
//System.out.println();
resultCommand=true;
}
else
resultCommand=false;
//throw Exception("Failed to execute command : " + cmd);
//System.out.println("Failed to execute command : " + cmd);
}
catch(Exception e){
resultCommand=false;
System.out.println("Exception : " + e.getMessage());
}
return theOutput;
}
private boolean sendRestart(String command) {
try {
// Open a session channel
System.out.println("Con7");
SessionChannelClient session = ssh.openSessionChannel();
System.out.println("Con8");
// Request a pseudo terminal, if you do not you may not see the prompt
if(session.requestPseudoTerminal("ansi", 80, 24, 0, 0, "")) {
System.out.println("Con8a");
// Start the users shell
if(session.startShell()) {
System.out.println("Con8b");
// Do something with the session output
//session.getOutputStream().write(command+"\n");
boolean r = session.executeCommand(command);
System.out.println("Con8c");
return r;
}
System.out.println("Con8d");
}
}
catch (IOException ex) {
System.out.println("Con8e");
ex.printStackTrace();
}
System.out.println("Con8f");
return false;
}
public String getOutputCommand(){
return outputCommand;
}
}