begini codingannya,walaupun ngacak dan berantakan (maklum,blom paham tentang OO)tapi ini karya sendiri.
package com.andia.server.poweroff;
import java.io.ByteArrayInputStream;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class Executed {
public static void main(String[] args) throws JSchException {
JSch jsch = new JSch();
String user = "root";
String pass = "aryandie";
String host = "192.168.0.195";
final Session session = jsch.getSession(user, host, 22);
session.setPassword(pass);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
System.out.println("connect ke server");
/*
* perintah eksekusi matiin server
*/
final String command = "init 0";
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(System.in);
((ChannelExec) channel).setErrStream(System.err);
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setText("Please Enter ");
CLabel text = new CLabel(shell, SWT.CENTER);
text.setText("IP Address : 192.168.0.195");
/*
* button shutdown
*/
final Button button = new Button(shell, SWT.PUSH);
button.setText("Init 0");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Channel channel = null;
try {
channel = session.openChannel("shell");
} catch (JSchException e1) {
e1.printStackTrace();
}
channel.setInputStream(System.in);
ByteArrayInputStream bi = new ByteArrayInputStream("init 0\r"
.getBytes());
channel.setInputStream(bi);
try {
channel.connect(30000);
} catch (JSchException e1) {
e1.printStackTrace();
}
}
});
/*
* button restart
*/
final Button button2 = new Button(shell, SWT.PUSH);
button2.setText("init 6");
button2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Channel channel = null;
try {
channel = session.openChannel("shell");
} catch (JSchException e1) {
e1.printStackTrace();
}
channel.setInputStream(System.in);
ByteArrayInputStream bi = new ByteArrayInputStream("init 6\r"
.getBytes());
channel.setInputStream(bi);
try {
channel.connect(30000);
} catch (JSchException e1) {
e1.printStackTrace();
}
}
});
shell.setLayout(new GridLayout(1, false));
shell.setSize(230, 130);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
setelah di coba hasilnya begini :
ket : init 0 = poweroff
init 6 = reboot
No comments:
Post a Comment