static public SshConnect sshConnect(URI uri) { return sshConnect(new MutableUri(uri)); }
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 public Target connect(String uri) throws IOException { SshSession ssh = sshConnect(uri); SshSftpSession sftp = SecureShells.sshSftp(ssh).run(); return probe(ssh, sftp); }
static public SshConnect sshConnect(String uri) { return sshConnect(MutableUri.of(uri)); }
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 public SshSession sshConnect(String uri) throws IOException { MutableUri u = MutableUri.of(uri); if (u.getScheme() == null) { throw new IOException("uri missing scheme (not in format such as ssh://host)"); } switch (u.getScheme()) { case "ssh": return SecureShells.sshConnect(uri).run(); case "vagrant+ssh": { String host = u.getHost(); log.info("Querying vagrant ssh-config for {}", host); return SecureShells.sshConnect("ssh://" + host) .configFile(VAGRANT_CLIENT.sshConfig(host)) .run(); } default: fail("Unsupported target uri. Support for either ssh://host or vagrant+ssh://host"); return null; } }
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; } }