initial
This commit is contained in:
commit
b8e528dfff
5 changed files with 383 additions and 0 deletions
117
CommandExecutor.java
Normal file
117
CommandExecutor.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue