The ssh Package: Secure Shell (SSH) Client for R

By rOpenSci – open tools for open science

cover image

(This article was first published on rOpenSci – open tools for open science, and kindly contributed to R-bloggers)

Have you ever needed to connect to a remote server over SSH to transfer files via SCP or to setup a secure tunnel, and wished you could do so from R itself? The new rOpenSci ssh package provides a native ssh client in R allows you to do that and even more, like running a command or script on the host while streaming stdout and stderr directly to the client. The package is based on libssh, a powerful C library implementing the SSH protocol.


Because the ssh package is based on libssh it does not need to shell out. Therefore it works natively on all platforms without any runtime dependencies. Even on Windows.

The package is still work in progress, but the core functionality should work. Below some examples to get you started from the intro vignette.

Connecting to an SSH server

First create an ssh session by connecting to an SSH server.

## connected:
## server: 1e:28:44:af:84:91:e5:88:fe:82:ca:34:d7:c8:cf:a8:0d:2f:ec:af

Once established, a session is closed automatically by the garbage collector when the object goes out of scope or when R quits. You can also manually close it using ssh_disconnect() but this is not strictly needed.


The client attempts to use the following authentication methods (in this order) until one succeeds:

  1. try key from privkey argument in ssh_connect() if specified
  2. if ssh-agent is available, try private key from ssh-agent
  3. try user key specified in ~/.ssh/config or any of the default locations: ~/.ssh/id_ed25519, ~/.ssh/id_ecdsa, ~/.ssh/id_rsa, or .ssh/id_dsa.
  4. Try challenge-response password authentication (if permitted by the server)
  5. Try plain password authentication (if permitted by the server)

To debug authentication set verbosity to at least level 2 or 3:


Execute Script or Command

Run a command or script on the host and block while it runs. By default stdout and stderr are steamed directly back to the client. This function returns the exit status of the remote command (hence it does not automatically error for an unsuccessful exit status).


You can also run a script that consists of multiple commands.

ssh_exec_wait(session, command = c(
  'curl -O',
  'R CMD check jsonlite_1.4.tar.gz',
  'rm -f jsonlite_1.4.tar.gz'
##   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
##                                  Dload  Upload   Total   Spent    Left  Speed
## 100 1071k  100 1071k    0     0   654k      0  0:00:01  0:00:01 --:--:--  654k
## * using log directory '/home/jeroen/jsonlite.Rcheck'
## * using R version 3.4.3 (2017-11-30)
## * using platform: x86_64-pc-linux-gnu (64-bit)
## * using session charset: ASCII
## * checking for file 'jsonlite/DESCRIPTION' ... OK
## * this is package 'jsonlite' version '1.4'
## * checking package namespace information ... OK
## * checking package dependencies ...

Capturing output

The ssh_exec_internal() is a convenient wrapper for ssh_exec_wait() which buffers the output steams and returns them as a raw vector. Also it raises an error by default when the remote command was not successful.

out  rnorm(10)
##  [1]  0.14301778 -0.26873489  0.83931307  0.22034917  0.87214122 -0.13655736
##  [7] -0.08793867 -0.68616146  0.23469591  0.93871035

This function is very useful if you are running a remote command and want to use it’s output as if you had executed it locally.

Using sudo

Note that the exec functions are non interactive so they cannot prompt for a sudo password. A trick is to use -S which reads the password from stdin:


Be very careful with hardcoding passwords!

Transfer Files via SCP

Upload and download files via SCP. Directories are automatically traversed as in scp -r.

# Upload a file to the server

This will upload the file to the home directory on your server. Let’s download it back:

# Download the file back and verify it is the same
scp_download(session, "COPYING", to = tempdir())
## 18011 /var/folders/l8/bhmtp25n2lx0q0dgv1x4gf1w0000gn/T//Rtmpldz4eO/COPYING

We can compare the checksums to verify that the files are identical:

##  "eb723b61539feef013de476e68b5c50a" 
tools::md5sum(file.path(tempdir(), "COPYING"))
##  "eb723b61539feef013de476e68b5c50a" 

Hosting a Tunnel

Opens a port on your machine and tunnel all traffic to a custom target host via the SSH server.

ssh_tunnel(session, port = 5555, target = "")

This function blocks while the tunnel is active. Use the tunnel by connecting to localhost:5555 from a separate process. The tunnel can only be used once and will automatically be closed when the client disconnects.

tried it for 2 weeks and absolutely love it! Thanks a lot for developing such nice packages

— aurelien ginolhac ♮ (@kingsushigino) June 5, 2018

To leave a comment for the author, please follow the link and comment on their blog: rOpenSci – open tools for open science. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…

Source:: R News