tl;dr: how can I get I/O when sending an exec command using sshj? Alternatively, what other Java SSH libraries provide a similar level of abstraction but might work better for my use case?
Apart from not working for me, yet, I really like sshj’s level of abstraction, most other Java/SSH libs are much lower level and I neither want nor need this for my use case.
Details:
As part for a tool for developer onboarding, I’m trying to install ssh public keys on a gitblit server, patterned after https://github.com/hierynomus/sshj/blob/master/examples/src/main/java/net/schmizz/sshj/examples/Exec.java.
I’m working with sshj:
compile 'com.hierynomus:sshj:0.21.1'
Platform: Koltin 1.1.3-2 (JRE 1.8.0_131-b11, Azul Zulu on Mac 10.12)
Unfortunately I can’t seem to read from or write to the streams.
I cut it down to this in the Repl, now just trying to get gitblits help message:
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.util.logging.Level
import java.util.logging.LogManager
fun sshClientWithTrustingHostkeyVerifier(): SSHClient {
return SSHClient(DefaultConfig()).also { client ->
client.addHostKeyVerifier(object :
OpenSSHKnownHosts(File(sshDir(), "known_hosts")) {
override fun hostKeyUnverifiableAction(hostname: String?, key: PublicKey?): Boolean {
super.write(SimpleEntry(null, hostname, KeyType.fromKey(key), key))
return true
}
})
}
}
LogManager.getLogManager().let {lm ->
lm.loggerNames.asSequence().forEach { lm.getLogger(it).apply{
level = Level.FINEST
handlers.forEach { it.level = Level.FINEST }}}}
val log: Logger = LoggerFactory.getLogger("foo")
log.debug("a")
val client = sshClientWithTrustingHostkeyVerifier()
log.debug("b")
client.connect("the.gitblit.local", 29418)
log.debug("c")
client.authPassword("my_account", "my super password")
log.debug("d")
val session = client.startSession()
log.debug("e")
val cmd = session.exec("keys --help")
log.debug("f")
log.debug(cmd.inputStream.readBytes().toString())
log.debug("g")
log.debug(cmd.errorStream.readBytes().toString())
log.debug("h")
cmd.join()
log.debug("i")
log.debug("Exit status: {}", cmd.exitStatus)
This produces a lot of output, which includes a strange-sounding error "[PROTOCOL_ERROR] Received CHANNEL_SUCCESS". I posted the full output on https://pastebin.com/d3Y1tc8r.
If I then let it sit, after a while it dies of a timeout. No output ever materializes.