pod.epiccastle.bbssh.core
Basic connection, execution, shell and copying functionality.
exec
(exec session-or-process command & [{:keys [agent-forwarding pty in in-enc out out-enc err err-enc pipe-buffer-size session no-connect], :or {pipe-buffer-size 8192, in-enc "utf-8", out-enc "utf-8", err-enc "utf-8"}, :as options}])Execute a command on the remote host over the ssh session.
Note: If
sessionis detected as a map like structure, it is assumed to be ababashka.processProcess. This enables threading ssh processes in with localbabashka.processprocesses. In such a situation you need to pass the session reference in via the:sessionkey inoptions.
options should be a hashmap with the following keys:
:sessionOverride the value passed into the first argument. Use this as the ssh session instead.:agent-forwardingIf set totrueenables ssh authentication agent for this command.:ptyIf set totrueallocate a pseudo terminal for this command execution.:inSpecify the data to be passed to stdin of the process. Can be:nil: (default) Pass nothing into stdin of the process and immediately close the input pipe.- a string: Pass this string into the stdin of the process (using encoding
:in-enc) and then close the input pipe. - a byte array: Pass this data into the stdin of the process and then close the input pipe.
- babashka InputStream instance: Use this instance to provide streaming data to the stdin of the process (the
.readmethod will be repeatedly called to provide data) - bbssh input-stream reference: Use this instance to provide streaming data to the stdin of the process (the
input-stream/readfunction will be repeatedly called to provide data) :stream: A PipedOutputStream/PipedInputStream instance pair is created and the PipedOutputStream will be returned in the:inkey of the result (for you to.writeorclojure.java.io/copyto).
:in-encIf:inis:stringthen optionally set the input encoding with this. Defaults to “utf-8”.:outSpecify how you want the output to be returned. The values can be::string: the returned:outvalue will be a future that will deref to a string of data. The future deref will block until the process is complete and the processes output is finished.:bytes: the returned:outvalue will be a future that will deref to a byte array of data. The future deref will block until the process is complete and the processes output is finished.- babashka OutputStream instance: The OutputStream will have its
.writemethod called as data becomes available from the process. - bbssh output-stream reference: The pod side output-stream will be written to with
output-stream/writeas data becomes available from the process. :stream(default): A PipedOutputStream/PipedInputStream instance pair is created and the PipedInputStream will be returned in the:outkey of the result (for you to.readorclojure.java.io/copyfrom).
:out-encIf:outis:stringthen optionally set the output encoding with this. Defaults to “utf-8”.:errSpecify how you want the output to be returned. The values can be::string: the returned:errvalue will be a future that will deref to a string of data. The future deref will block until the process is complete and the processes output is finished.:bytes: the returned:errvalue will be a future that will deref to a byte array of data. The future deref will block until the process is complete and the processes output is finished.- babashka OutputStream instance: The OutputStream will have its
.writemethod called as data becomes available from the process. - bbssh output-stream reference: The pod side output-stream will be written to with
output-stream/writeas data becomes available from the process. :stream(default): A PipedOutputStream/PipedInputStream instance pair is created and the PipedInputStream will be returned in the:errkey of the result (for you to.readorclojure.java.io/copyfrom).
:err-encIf:erris:stringthen optionally set the output encoding with this. Defaults to “utf-8”.:pipe-buffer-sizewhen:in,:outor:errare set to:stream, this value can be passed an integer to set the pipe buffer size. Larger values create more chunked output but increase through-put. Default is 8192.:no-connectwhentruedo not connect this channel to the command. Simply set it up and return it. You will need to callchannel-exec/connecton the channel to actually initiate the process and channel.
ssh
(ssh hostname & [{:keys [agent port username password identity passphrase private-key public-key strict-host-key-checking config-file known-hosts accept-host-key connection-options port-forward-local port-forward-remote no-connect identity-repository user-info host-key-repository proxy], :or {port 22, strict-host-key-checking :ask, accept-host-key false, connection-options {}}, :as options}])Start an SSH session. If connection is successful, returns the SSH session reference. If connection is unsuccessful, raises an exception. Requires a string for hostname. Pass in an optional hashmap with the following keys:
:portThe port to connect to. Defaults to 22.:usernameThe username to connect as. Defaults to the current username.:passwordPass in a string to use for password authentication.:identityPass in a file or filename of the private key to use in key authentication.:passphrasePass in a string to decrypt the private key with if needed.:private-keyPass in a string (base64) or byte array of the private key to use for authentication.:public-keyPass in a string (base64) or byte array of the public key associated with the private-key:strict-host-key-checkingControl strict host key checking. If set totrue, bbssh will never add host keys to known host and will refuse to connect to hosts whose host key has changed. If set tofalse, bbssh will allow connection to hosts with unknown or changed keys. If the key is unknown it will add it to known hosts, but it will not change a key if it is present. A value of:askwill mean new host keys will be added to the known hosts, or a changed key modified, only after the user has confirmed this is what they wish to do via the terminal. Default is:ask:config-fileA string or java.io.File referencing an OpenSSH config file to use for configuration settings. Defaults to"~/.ssh/config". Passing infalsedisables any config file.:port-forward-localA vector of local port forwarding directives. Each element should be a hashmap with the keys:local-port,:remote-hostand:remote-port. All connections to:local-porton the local machine will be forwarded to:remote-host:remote-porton the remote machine. Instead of setting:remote-hostand:remote-portyou may set:remote-unix-socketto the remote file path of a unix domain socket to forward to that. An optional key of:bind-addresscan be used to control the local interface that is bound. Defaults to 127.0.0.1. Optional key:connect-timeoutdefines how many milliseconds to persist in attempting the remote connection. Defaults to 0 (try forever).:port-forward-remoteA vector of remote port forwarding directives. Each element should be a hashmap with the keys:remote-port,:local-hostand:local-port. All connections to:remote-porton the remote machine will be forwarded to:local-host:local-porton the local machine. An optional key of:bind-addresscan be used to control the remote interface that is bound. Defaults to 127.0.0.1. Note: remote port forwarding cannot be setup until the connection is made. Thus setting:no-connectto true will prevent:port-forward-remotefrom initiating.:known-hostsA string defining the path to the known_hosts file to use. It is set to ~/.ssh/known_hosts by default. Set tofalseto disable using a known hosts file.:accept-host-keyWhen:strict-host-key-checkingis set to:askthis setting controls the adding of the key. If unset, or set tonilthe default behavior of asking the user via the terminal applies. Iftrueaccept the host key if it is unknown or changed. Iffalsereject the connection if the host key is unknown or changed. If set to a string, accept the host-key only if the key fingerprint matches the string. If set to:new, accept the key if it is unknown, but reject it if it has changed. Defaults tonil.:connection-optionsA hashmap of lesser used connection options. See below for details.:no-connectSet to true to prevent the connection from being initiated. Just returns the prepared session reference. You will then need to callsession/connecton it to initiate the connection.:agentThe bbssh agent to use to construct the session. If none is supplied a new bbssh agent will be created. Note: this is not an ssh-agent. It is the root class that contains all the session instances and settings.:identity-repositoryUse a custom identity-repository in the connection.:user-infoUse a custom user-info in the connection. Specifying this value will override:accept-host-keyand change the behavior of password and passphrase prompts (your user-info functions will be called instead).:host-key-repositoryUse a custom host-key-repository in the connection. Specifying this value will override:known-hostssetting (your host-key-repository functions will be called instead).:proxyUse a http or socks proxy to make the connection. The provided arg must have at least:type(one of#{:http :socks4 :socks5}),:host,:portand optionally:usernameand:passwordfor proxy authentication.:silence-messagesWhen set to true do not echo SSH messages to stdout in the default user-info. Such messages include any Banner setup on the server. Only has an effect if you are not using a custom user-info.
The hashmap passed in :connection-options can have the following keys. Each key takes a string or a function as a configuration value. If passed a string, that string becomes the set value. If passed a function this function should take a single string as an argument and return a string. It will be passed the existing configuration setting and should return the new configuration setting. These can be used to append or prepend extra values onto the default settings.
:kex, :server-host-key, :prefer-known-host-key-types, :enable-server-sig-algs, :cipher, :cipher-s2c, :cipher-c2s, :mac, :mac-s2c, :mac-c2s, :compression, :compression-s2c, :compression-c2s, :lang, :lang-s2c, :lang-c2s, :dhgex-min, :dhgex-max, :dhgex-preferred, :compression-level, :preferred-authentications, :client-pubkey, :check-ciphers, :check-macs, :check-kexes, :check-signatures, :fingerprint-hash, :max-auth-tries