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.
session ## connected: email@example.com:22 ## 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:
- try key from
- if ssh-agent is available, try private key from ssh-agent
- try user key specified in
~/.ssh/configor any of the default locations:
- Try challenge-response password authentication (if permitted by the server)
- 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 https://cran.r-project.org/src/contrib/Archive/jsonlite/jsonlite_1.4.tar.gz', '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 ... ...
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) ##  0.14301778 -0.26873489 0.83931307 0.22034917 0.87214122 -0.13655736 ##  -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.
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
# Upload a file to the server file_path
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:
tools::md5sum(file_path) ## "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 = "ds043942.mongolab.com:43942")
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
R-bloggers.com 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