SSH connections, running a command on a server, and working with files over SFTP.
import ssh
This package is an SSH client written in Salam and needs neither OpenSSH nor any other library. Make a Config with the host and the user, pick the login method, and connect with Connect; then run a command with Exec, or move files with SftpOpen. It suits automating deployments, backups and checks across a fleet of servers. It is a client only; for plain file transfer with an FTP server see ftp.
The host key is checked against the known_hosts file, the way ssh itself does. The buffer and reader functions build and read the data format of the SSH protocol; an ordinary program does not need them.
What it supports, and what it does not:
•
Keys must be RSA, in the OpenSSH format, without a passphrase. Ed25519 and ECDSA keys, the default of recent ssh-keygen, are not read. Make a key for automation with ssh-keygen -t rsa -b 4096 -N "" -f deploy_key; a password login with WithPassword also works.
•
The server needs an RSA host key; a server that offers only Ed25519 or ECDSA host keys cannot be checked and the connection fails.
•
Key exchange is curve25519 or Diffie-Hellman group 14, and the ciphers are aes128-ctr and aes256-ctr, which every OpenSSH server accepts. There is no agent support and no port forwarding; an interactive shell with a terminal is available through NewSession, RequestPty and Shell.
•
Each Exec is a fresh shell: a cd or an exported variable does not carry over to the next call.
Run a deployment, step by step
Exec runs one command and waits for it, returning its standard output, its standard error and the exit code in an ExecResult. Each call opens a new channel, so a cd in one command does not carry over to the next; join commands with && when they belong together. A deployment is then a list of steps that stops at the first one whose exit code is not zero.
This needs a real server, so it is only checked here. WithKeyFile only reads the file, so it returns false when the path is wrong; a key the package cannot use makes Connect fail instead. For a command that runs for a long time and whose output you want as it comes, such as tail -f, use NewSession, Start and ReadStep with DrainOutput instead, as the example in the reference shows.
Pull a backup over SFTP
SSH and SFTP share one connection: run the command that writes the file with Exec, then open SFTP on the same client and fetch it. Stat gives the size, Download copies the file, and Remove cleans up on the server once the copy has arrived. For whole directories there are UploadDir, DownloadDir and MkdirAll.
The host key is how the client knows it is talking to the right machine and not to someone in between. The mode decides what happens with a key that is not in known_hosts yet. HOSTKEY_MODE_STRICT refuses it, which is right for automation: add the key once by hand, then every later change is an error you want to hear about. HOSTKEY_MODE_ACCEPT_NEW is the default; it records a new key the first time and refuses a changed one later, like StrictHostKeyChecking=accept-new in OpenSSH. HOSTKEY_MODE_INSECURE checks nothing and belongs only in tests.
With an empty known_hosts the first, strict attempt is refused, the second records the key, and the third, strict again, now connects. HostKeyFingerprint returns the same SHA-256 fingerprint that ssh-keygen -lf prints, so you can compare it with what the server's administrator gave you. The client checks the server's RSA host key (rsa-sha2-256, rsa-sha2-512 or ssh-rsa), so the server needs one; a default OpenSSH install usually has it.
Many hosts from one list
A Config is a plain value, so it is easy to build one per line of an inventory and keep the same rules for all of them: the same user, the same known_hosts file and strict checking. Nothing connects until Connect is called, so the program below runs without a network.
deploy@web1.example.com port 22 strict: true
deploy@web2.example.com port 22 strict: true
ops@bastion.example.com port 2222 strict: true
To run the same command on all hosts, loop over the configs, connect, Exec and Close. If one host is down, Ok is false for that client and Error says why, and the loop can go on to the next.
Reads the private key from a file and switches the login to that key; false when the file could not be read or is empty. The key must be an unencrypted RSA key in the OpenSSH format; any other key is only refused at Connect.
The example for this item is shown with the ones above: ssh-Config.salam