This commit is contained in:
rasta5man 2025-04-16 21:46:50 +02:00
commit b8e528dfff
5 changed files with 383 additions and 0 deletions

72
BuildServerArray.java Normal file
View file

@ -0,0 +1,72 @@
package handleSsh;
import java.util.ArrayList;
import java.util.HashMap;
public class BuildServerArray {
private HashMap<String,String> servers = new HashMap<>();
private ArrayList<Server> allServers = new ArrayList<>();
public BuildServerArray() {
// ALL SENICA rvo
// servers.put("10.0.0.130","unipi");
servers.put("10.0.0.119","unipi");
servers.put("10.0.0.136","unipi");
servers.put("10.0.0.112","unipi");
// servers.put("10.0.0.138","unipi");
// servers.put("10.0.0.123","unipi");
// servers.put("10.0.0.124","unipi");
// servers.put("10.0.0.131","unipi");
// servers.put("10.0.0.109","unipi");
// servers.put("10.0.0.128","unipi");
// servers.put("10.0.0.110","unipi");
// servers.put("10.0.0.127","unipi");
// servers.put("10.0.0.129","unipi");
// servers.put("10.0.0.111","unipi");
// servers.put("10.0.0.122","unipi");
// servers.put("10.0.0.132","unipi");
// servers.put("10.0.0.117","unipi");
// servers.put("10.0.0.118","unipi");
// servers.put("10.0.0.120","unipi");
// servers.put("10.0.0.133","unipi");
// servers.put("10.0.0.134","unipi");
// servers.put("10.0.0.135","unipi");
// servers.put("10.0.0.107","unipi");
// servers.put("10.0.0.139","unipi");
// servers.put("10.0.0.137","unipi");
// servers.put("10.0.0.140","unipi");
// servers.put("10.0.0.141","unipi");
// 24/7
// servers.put("10.0.0.115","unipi");
servers.put("10.0.0.114","unipi");
// servers.put("10.0.0.126","unipi");
// servers.put("10.0.0.113","unipi");
// servers.put("10.0.0.121","unipi");
// servers.put("10.0.0.116","unipi");
//test
// servers.put("10.0.0.38","unipi");
// servers.put("10.0.0.5","lm");
for (String ip : servers.keySet()) {
if(servers.get(ip).equals("unipi"))
{
allServers.add(new ServerBuilder().host(ip).buildServer());
}
else
{
allServers.add(new ServerBuilder().host(ip).username("root").password("admin").path("/root").buildServer());
}
}
}
public ArrayList<Server> getAllServers() {
return allServers;
}
}

117
CommandExecutor.java Normal file
View file

@ -0,0 +1,117 @@
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;
public class CommandExecutor {
private Session session;
private final String fileToRead = "monitor.txt";
private String commandsToExecute = "";
private ArrayList<Server> allServers = new ArrayList<>();
public CommandExecutor() {
allServers = new BuildServerArray().getAllServers();
// commandsToExecute = "tail -n 20 flowserver/" + fileToRead;
// commandsToExecute = "ls -l flowserver/";
// commandsToExecute = "sudo journalctl --vacuum-size=1M";
// commandsToExecute = "sudo rm flowserver/monitor.1.txt.gz";
commandsToExecute = "sudo service nodejs restart";
// commandsToExecute = "sudo service nodejs stop";
}
public void open(String username, String hostname, String password) throws JSchException {
JSch jSch = new JSch();
session = jSch.getSession(username, hostname, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no"); // not recommended
session.setConfig(config);
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) {
CommandExecutor ssh = new CommandExecutor();
for (Server s : ssh.allServers) {
try {
ssh.open(s.getUsername(), s.getHost(), s.getPassword());
// String ret = ssh.runCommand("tail -n 20 " + s.getPath() + "/flowserver/" + ssh.fileToRead);
// String ret = ssh.runCommand("ls -la /home/unipi/flowserver/flow");
// String ret = ssh.runCommand("sudo service nodejs restart");
String ret = ssh.runCommand(ssh.commandsToExecute);
System.out.println(ret);
ssh.close();
} catch (JSchException | IOException e) {
System.out.println(e);
//throw new RuntimeException(e);
}
}
}
}

40
Server.java Normal file
View file

@ -0,0 +1,40 @@
package handleSsh;
public class Server {
private String host;
private String username;
private String password;
private String path;
private int port;
public Server(String host, String username, String password, String path, int port) {
this.host = host;
this.username = username;
this.password = password;
this.path = path;
this.port = port;
}
public String getHost() {
return host;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getPath() {
return path;
}
public int getPort() {
return port;
}
@Override
public String toString() {
return "Server{" +
"host='" + host + '\'' +
'}';
}
}

49
ServerBuilder.java Normal file
View file

@ -0,0 +1,49 @@
package handleSsh;
public class ServerBuilder {
private String host = null;
private String username = "unipi";
private String password = "unipi.technology";
private String path = "/home/unipi";
private int port = 22;
public ServerBuilder() { }
public Server buildServer()
{
return new Server(host, username, password, path, port);
}
public ServerBuilder host(String host)
{
this.host = host;
return this;
}
public ServerBuilder username(String username)
{
this.username = username;
return this;
}
public ServerBuilder password(String password)
{
this.password = password;
return this;
}
public ServerBuilder path(String path)
{
this.path = path;
return this;
}
public ServerBuilder port(int port)
{
this.port = port;
return this;
}
}

105
UploadFiles.java Normal file
View file

@ -0,0 +1,105 @@
package handleSsh;
import com.jcraft.jsch.*;
import java.util.ArrayList;
public class UploadFiles {
// READ
// files to upload should always be in "flowserver" directory !!!
// RENAME IT if it is not
private static final String[] filesToUpload =
{
"/home/rasta5man/dev/oms/flowserver/flow/cloudmqttconnect.js",
"/home/rasta5man/dev/oms/flowserver/flow/cmd_manager.js",
"/home/rasta5man/dev/oms/flowserver/flow/db_init.js",
"/home/rasta5man/dev/oms/flowserver/flow/designer.json",
"/home/rasta5man/dev/oms/flowserver/flow/dido_controller.js",
"/home/rasta5man/dev/oms/flowserver/flow/infosender.js",
"/home/rasta5man/dev/oms/flowserver/flow/modbus_reader.js",
"/home/rasta5man/dev/oms/flowserver/flow/show_dbdata.js",
"/home/rasta5man/dev/oms/flowserver/flow/slack_filter.js",
"/home/rasta5man/dev/oms/flowserver/flow/thermometer.js",
"/home/rasta5man/dev/oms/flowserver/flow/wsmqttpublish.js",
"/home/rasta5man/dev/oms/flowserver/config",
"/home/rasta5man/dev/oms/flowserver/flow/helper/DataToTbHandler.js",
"/home/rasta5man/dev/oms/flowserver/flow/helper/ErrorToServiceHandler.js",
"/home/rasta5man/dev/oms/flowserver/flow/helper/logger.js",
"/home/rasta5man/dev/oms/flowserver/flow/helper/notification_reporter.js",
"/home/rasta5man/dev/oms/flowserver/flow/helper/serialport_helper.js",
"/home/rasta5man/dev/oms/flowserver/databases/notifications.table",
"/home/rasta5man/dev/oms/flowserver/databases/tbdatacloud.nosql",
//"/home/rasta5man/dev/oms/flowserver/databases/modbus_config.js",
//"/home/rasta5man/dev/oms/flowserver/databases/status.table"
};
private static String buildDestinationDirectory(String fileToUpload) {
int index = fileToUpload.indexOf("/flowserver");
return fileToUpload.substring(index + 11);
}
private static ArrayList<Server> allServers;
public static void main(String[] args) {
allServers = new BuildServerArray().getAllServers();
for (Server server : allServers) {
System.out.println("Connecting to server: " + server);
copyFileToServer(server);
}
}
private static void copyFileToServer(Server server) {
JSch jsch = new JSch();
Session session = null;
ChannelSftp sftpChannel = null;
try {
session = jsch.getSession(server.getUsername(), server.getHost(), server.getPort());
session.setPassword(server.getPassword());
session.setConfig("StrictHostKeyChecking", "no"); // Avoids checking for new host keys (not recommended for production)
session.connect();
sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
for (String fileToUpload : filesToUpload) {
System.out.println("Uploading " + fileToUpload + " to " + server);
String directoryToUpload = "";
if(server.getUsername().equals("root")) // je to LM
{
directoryToUpload = "/root/flowserver" + buildDestinationDirectory(fileToUpload);
}
else
{
directoryToUpload = "/home/unipi/flowserver" + buildDestinationDirectory(fileToUpload);
}
sftpChannel.put(fileToUpload, directoryToUpload);
}
} catch (JSchException | SftpException e) {
System.err.println("Error connecting to " + server.getUsername() + ": " + e.getMessage());
} finally {
if (sftpChannel != null) {
sftpChannel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
}