1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public static void execCommandWithArguments(String command,String user,String host,String password, String args) throws JSchException, IOException, InterruptedException{
Session session = getSSHSession(user, host, password);
Channel channel = session.openChannel("shell");
//Add the command execution end keyword
command +=" "+args;
command += END_COMMAND;
File sshLogFile = new File(LOG_LOCATION);
sshLogFile.createNewFile();
sshLogFile.deleteOnExit();
FileOutputStream fileOutputStream = new FileOutputStream(sshLogFile);
channel.setOutputStream(fileOutputStream);
PipedInputStream pip = new PipedInputStream();
channel.setInputStream(pip);
PipedOutputStream pop = new PipedOutputStream(pip);
PrintStream print = new PrintStream(pop);
print.println(command);
channel.connect();
//Blocking method waiting for the command execution to end before close the ssh session
waitForTheEnd(sshLogFile);
channel.disconnect();
} |
Partager