plc-controller/CommandExecutor.java
2025-08-07 15:52:03 +02:00

128 lines
4.3 KiB
Java

package handleSsh;
import com.jcraft.jsch.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
public class CommandExecutor {
private Session session;
private String commandsToExecute = "";
private int timeBetweenCommands;
private final ArrayList<Server> allServers;
public CommandExecutor() {
allServers = new BuildServerArray().getAllServers();
timeBetweenCommands = 30; // in seconds (if rebooting, put cca 20 seconds between reboots)
commandsToExecute = "sudo journalctl --vacuum-size=1M";
commandsToExecute = "sudo service nodejs restart";
commandsToExecute = "python /root/flowserver/addSwitch.py";
commandsToExecute = "python3 /home/unipi/flowserver/addSwitch.py";
commandsToExecute = "sudo service nodejs stop";
commandsToExecute = "tail -n 10 flowserver/monitor.txt";
commandsToExecute = "sudo reboot";
// commandsToExecute = "ls -l /home/unipi/flowserver/databases";
// commandsToExecute = "ls -l /root/flowserver/databases";
}
public void open(String username, String hostname, String password) throws JSchException {
JSch jSch = new JSch();
jSch.setKnownHosts("/home/rasta5man/.ssh/known_hosts");
jSch.addIdentity( "/home/rasta5man/.ssh/oms_key" );
session = jSch.getSession(username, hostname, 22);
// Properties config = new Properties();
//config.put("StrictHostKeyChecking", "no"); // not recommended
//session.setConfig("StrictHostKeyChecking", "no"); // not recommended
// session.setConfig("PreferredAuthentications", "password");
//session.setConfig(config);
//session.setConfig("PreferredAuthentications", "password,keyboard-interactive");
session.setPassword(password);
System.out.println("Connecting SSH to " + hostname + " - Please wait for few seconds... ");
session.connect();
System.out.println("Connected!");
}
public String runCommand(String command) throws JSchException, IOException {
String ret = "";
if (!session.isConnected())
throw new RuntimeException("Not connected to an open session. Call open() first!");
ChannelExec channel = null;
channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
PrintStream out = new PrintStream(channel.getOutputStream());
InputStream in = channel.getInputStream(); // channel.getInputStream();
channel.connect();
ret = getChannelOutput(channel, in);
channel.disconnect();
System.out.println("Finished sending commands!");
return ret;
}
private String getChannelOutput(ChannelExec channel, InputStream in) throws IOException {
byte[] buffer = new byte[1024];
StringBuilder strBuilder = new StringBuilder();
String line = "";
while (true) {
while (in.available() > 0) {
int i = in.read(buffer, 0, 1024);
if (i < 0) {
break;
}
strBuilder.append(new String(buffer, 0, i));
System.out.println(line);
}
if (channel.isClosed()) {
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
return strBuilder.toString();
}
public void close() {
session.disconnect();
System.out.println("Disconnected channel and session");
}
public static void main(String[] args) throws InterruptedException {
CommandExecutor ssh = new CommandExecutor();
for (Server s : ssh.allServers) {
try {
ssh.open(s.getUsername(), s.getHost(), s.getPassword());
String ret = ssh.runCommand(ssh.commandsToExecute);
System.out.println(ret);
ssh.close();
} catch (JSchException | IOException e) {
System.out.println(e);
//throw new RuntimeException(e);
}
TimeUnit.SECONDS.sleep(ssh.timeBetweenCommands);
}
}
}