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 allServers; public CommandExecutor() { allServers = new BuildServerArray().getAllServers(); timeBetweenCommands = 1; // in seconds (if rebooting, put cca 20 seconds between reboots) // commandsToExecute = "sudo journalctl --vacuum-size=1M"; // commandsToExecute = "sudo service nodejs restart"; // commandsToExecute = "rm -rf /home/unipi/flowserver/flow"; // commandsToExecute = "mkdir -p /home/unipi/flowserver/flow/helper"; commandsToExecute = "python /root/flowserver/addSwitch.py"; commandsToExecute = "python3 /home/unipi/flowserver/addSwitch.py"; commandsToExecute = "sudo service nodejs stop"; commandsToExecute = "tail -n 3 flowserver/monitor.txt"; // commandsToExecute = "sudo reboot"; // commandsToExecute = "ls -l /home/unipi/flowserver/databases"; // commandsToExecute = "ls -l /root/flowserver/databases"; // commandsToExecute = "rm /home/unipi/flowserver/databases/1.tbdata.nosql /home/unipi/flowserver/databases/2.tbdata.nosql /home/unipi/flowserver/databases/3.tbdata.nosql /home/unipi/flowserver/databases/4.tbdata.nosql /home/unipi/flowserver/databases/5.tbdata.nosql /home/unipi/flowserver/databases/6.tbdata.nosql /home/unipi/flowserver/databases/7.tbdata.nosql /home/unipi/flowserver/databases/8.tbdata.nosql /home/unipi/flowserver/databases/9.tbdata.nosql /home/unipi/flowserver/databases/10.tbdata.nosql"; // commandsToExecute = "head -n 1 /home/unipi/flowserver/databases/nodes_original/nodes_original.table"; // commandsToExecute = "sudo sh -c 'chmod 777 /etc/owfs.conf; chmod 777 /etc/fuse.conf; chmod 777 /etc/evok.conf; chmod 777 /lib/systemd/system/owfs.service; mkdir /mnt/1wire'"; // commandsToExecute = "sudo sh -c 'service owfs stop; service evok stop; service nodejs stop; chmod 777 /etc/owfs.conf; chmod 777 /etc/fuse.conf; chmod 777 /etc/evok.conf; chmod 777 /lib/systemd/system/owfs.service; mkdir /mnt/1wire'"; // commandsToExecute = "sudo sh -c 'chmod 644 /etc/owfs.conf; chmod 644 /etc/fuse.conf; chmod 644 /etc/evok.conf; chmod 644 /lib/systemd/system/owfs.service'"; // commandsToExecute = "python /home/unipi/flowserver/cloud_topic.py && rm /home/unipi/flowserver/databases/status.table"; } 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.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); } } }