public SshExec sshExec(boolean sudo, boolean shell, Object... args) { Deque<Object> arguments = new ArrayDeque<>(); if (sudo) { if (commands.containsKey("doas")) { arguments.add("doas"); } else { // man sudo // -S The -S (stdin) option causes sudo to read the password from the // standard input instead of the terminal device. arguments.add("sudo"); arguments.add("-S"); } } if (shell) { arguments.add("sh"); arguments.add("-c"); } arguments.addAll(Arrays.asList(args)); SshExec exec = SecureShells.sshExec(ssh); exec.command(arguments.pop().toString()); while (arguments.size() > 0) { exec.arg(arguments.pop()); } return exec; }
static private Map<String,String> which(SshSession ssh, List<String> commands) { // doesn't matter if we find it or not String whichString = sshExec(ssh, "which", commands.toArray()) .pipeOutput(Streamables.captureOutput()) .pipeError(Streamables.nullOutput()) .exitValues(0, 1, 2) .runResult() .map(Actions::toCaptureOutput) .asString(); if (whichString == null) { return Collections.emptyMap(); } Map<String,String> result = new HashMap<>(); String[] lines = whichString.split("\\\n"); for (String line : lines) { Path cmd = Paths.get(line.trim()); result.put(cmd.getFileName().toString(), line.trim()); } return result; }
static private String uname(SshSession ssh) { // doesn't matter if we find it or not return sshExec(ssh, "uname", "-srm") .pipeOutput(Streamables.captureOutput()) .pipeError(Streamables.nullOutput()) .exitValues(0) .runResult() .map(Actions::toCaptureOutput) .asString() .trim(); }
static private InitType initType(SshSession ssh) { String initTypeString = sshExec(ssh) .command("sh").args("-c", "if $(/sbin/init --version | egrep -q 'upstart'); then echo upstart; " + "elif $(systemctl | egrep -q '.mount'); then echo systemd; " + "elif [ -f /etc/init.d/cron ] || [ -f /etc/init.d/crond ]; then echo sysv; " + "else echo unknown; fi") .pipeOutput(Streamables.captureOutput()) .pipeError(Streamables.nullOutput()) .runResult() .map(Actions::toCaptureOutput) .asString(); if (initTypeString == null) { return null; } switch (initTypeString.trim()) { case "sysv": return InitType.SYSV; case "systemd": return InitType.SYSTEMD; case "upstart": return InitType.UPSTART; default: return InitType.UNKNOWN; } }