Ssh
Native SSH client in R based on libssh
Install / Use
/learn @ropensci/SshREADME
SSH
Secure Shell (SSH) Client for R
Installation
This package is available on CRAN and can be installed via:
install.packages('ssh')
Alternatively it can be installed from source using devtools:
remotes::install_github('ropensci/ssh')
Installation from source on MacOS or Linux requires libssh (the original libssh, not the unrelated libssh2 library). On Debian or Ubuntu use libssh-dev:
sudo apt-get install -y libssh-dev
On Fedora we need libssh-devel:
sudo yum install libssh-devel
On CentOS / RHEL we install libssh-devel via EPEL:
sudo yum install epel-release
sudo yum install libssh-devel
On OS-X use libssh from Homebrew:
brew install libssh
Using conda (need a conda R environment conda create -n Renv r-base r-essentials)
conda install --channel conda-forge r-ssh
If you have issues with the conda installation please submit an issue in conda-forge/r-ssh-feedstock
Getting Started
First create an ssh session by connecting to an SSH server. You can either use private key or passphrase authentication:
session <- ssh_connect("jeroen@dev.opencpu.org")
You can use the session in subsequent ssh functions below.
Run a command
Run a command or script on the host while streaming stdout and stderr directly to the client.
ssh_exec_wait(session, command = c(
'curl -fOL https://cloud.r-project.org/src/contrib/Archive/jsonlite/jsonlite_1.5.tar.gz',
'R CMD check jsonlite_1.5.tar.gz',
'rm -f jsonlite_1.5.tar.gz'
))
If you want to capture the stdout/stderr:
out <- ssh_exec_internal(session, "R -e 'rnorm(100)'")
cat(rawToChar(out$stdout))
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:
out <- ssh_exec_wait(session, 'echo "mypassword!" | sudo -s -S apt-get update -y')
Be very careful with hardcoding passwords!
Uploading and Downloading via SCP
Upload and download files via SCP. Directories are automatically traversed as in scp -r.
# Upload a file to the server
file_path <- R.home("COPYING")
scp_upload(session, file_path)
# Download the file back and verify it is the same
scp_download(session, "COPYING", to = tempdir())
tools::md5sum(file_path)
tools::md5sum(file.path(tempdir(), "COPYING"))
Create 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.
Disconnect
When you are done with the session you should disconnect:
ssh_disconnect(session)
If you forgot to disconnect, the garbage collector will do so for you (with a warning).
Related Skills
node-connect
351.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
