In order to realize a small supervisor of a server, I would like to know how to relaunch the ask(...) method in my code below. The goal is to query the server via SSH with the JSch library, and to return the result, in the JTextArea every x minutes.
The code works fine for me, I want just add the refresh option in the JTextArea, which receives the result via SSH every x minutes.
public class Test1 {
public static void main(String args[]) throws Exception {
final JFrame frame = new JFrame("Superviser SRV");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500);
JLabel label1 = new JLabel("Informations of SRV",JLabel.CENTER);
frame.getContentPane().add(label1);
JTextArea ta11 = new JTextArea(1,50);
JScrollPane sp11 = new JScrollPane(ta11);
Border border = BorderFactory.createLineBorder(Color.BLACK);
ta11.setBorder(BorderFactory.createCompoundBorder(border,
BorderFactory.createEmptyBorder(10, 10, 10, 10)));
frame.getContentPane().add(sp11);
frame.setVisible(true);
int Port=22;
String usernam ="usernam";
String passwd ="passwd";
String host ="192.168.10.1";
String msg = " Check Procs = ";
String com = " /usr/local/nagios/libexec/check_procs -w 200 -c 300 && exit \n";
ask(ta11, Port, usernam, host, passwd, msg, com);
}
public static void ask(JTextArea ta, int port, String name, String ip, String password, String msg, String com)
throws JSchException, IOException {
JSch jsch = new JSch();
Session session = jsch.getSession(name, ip, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
System.out.println("Establishing Connection...");
session.connect();
System.out.println("Connection established.");
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
InputStream in = channelExec.getInputStream();
channelExec.setCommand("sudo su - root");
channelExec.connect();
OutputStream out = channelExec.getOutputStream();
out.write((com).getBytes());
out.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line= reader.readLine();
ta.append(msg+" = "+line);
out.close();
reader.close();
channelExec.disconnect();
session.disconnect();
}
}