Comments (6)
R is single threaded so indeed you cannot run a tunnel and a client in the same R. But you can use something like the processx
or callr
to start a background R process for the tunnel:
proc <- callr::r_bg(function(){
session <- ssh::ssh_connect('your.server.org')
ssh::ssh_tunnel(session)
})
Or you can try the same thing with the sys package which prints progress to the parent R session:
str <- "session <- ssh::ssh_connect('dev.opencpu.org');ssh::ssh_tunnel(session)"
R <- R.home('bin/R')
pid <- sys::exec_background(R, c("-e", str))
from ssh.
Hi all, thanks for this useful discussion. I'm trying to connect to a postgres db using the code below.
cmd <- 'session <- ssh::ssh_connect("user@host");ssh::ssh_tunnel(session, port = 5555, target = "127.0.0.1:5432")'
pid <- sys::r_background(args = c("-e", cmd))
con <- DBI::dbConnect(
RPostgres::Postgres(),
dbname = "dbname",
host = "127.0.0.1",
port = 5555,
user = "user",
password = "pwd"
)
The tunnel sets up fine but when I run dbConnect I get the following message in the console:
client connected!
\ Tunneled 145 bytes...tunnel closed!
>
>
Error: could not connect to server: Connection reset by peer
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5555?
Warning message:
Disconnecting from unused ssh session. Please use ssh_disconnect()
It seems that the tunnel is disconnecting before the connection code has a chance to run inside it? It's strange because if I open the same tunnel in the terminal and then run the connection code in R it works.
Is there any way to prevent the tunnel closing? Thanks!
from ssh.
I'm having same issue, getting around by opening a different R session and connecting there. But would be great to not have to use that workaround.
from ssh.
@jeroen Do you have any suggestion on how to make the background process prompt for the password of an ssh key?
In my setup I'd like to use
ssh_connect('[email protected]', keyfile = '~/.ssh/id_rsa')
However, using your solution with callr
or sys
does not work, as the background process cannot raise the password prompt that unlocks the ssh key.
from ssh.
Did you try setting your passphrase in the password
argument in ssh_connect ()
? If you are on linux or macos the best solution is it load your key in ssh-agent.
ssh_agent_add("~/.ssh/id_rsa")
Once the key is loaded in the agent, the you should be able to connect to your servers without passing a keyfile or passphrase.
from ssh.
@jeroen Awesome, ssh_agent_add
works perfect. Thank you!!
A minor thing is that establishing the ssh tunnel takes a bit of time, but more than it takes for R to proceed. Do you have any idea how I could make sure R waits for the tunnel to be established before proceeding? At the moment I'm using Sys.sleep(1)
, but this doesn't seem quite right.
ssh::ssh_agent_add()
proc <- callr::r_bg(function(){
session <- ssh::ssh_connect('[email protected]')
ssh::ssh_tunnel(session, port = 5555, target = paste0('localhost:', 3306))
})
Sys.sleep(1)
con <- DBI::dbConnect(RMySQL::MySQL(),
username = 'user',
host = '127.0.0.1',
port = 5555,
password = rstudioapi::askForPassword('Database password'),
dbname = 'dbname')
from ssh.
Related Issues (20)
- problem using scp on a complete folder tree
- Package installation onto Docker HOT 1
- SSH failing to open Known host file HOT 1
- ssh_tunnel to localhost.run
- Error when using scp_upload() & scp_download on some SFTPs
- SSH session automatically disconnects after parallel programming
- cannot scp_upload from Windows to Windows HOT 1
- cannot use wildcards with scp_download
- Problems wit large files with scp_download
- Make SSH tunnel to query a remote redshift database in R HOT 1
- switch user using sudo failing HOT 1
- ssh-agent not being found HOT 1
- Feature Request: `ssh_info()` works on disconnected sessions
- SSH_AUTH_METHOD_PASSWORD does not seem to be working
- scp_upload problem in newest verision HOT 1
- Could add function support SSH without password HOT 1
- How do I preserve the mode permission of a file with scp_upload HOT 1
- scp_upload and scp_download fail for UNC paths without permission on root directory
- Uploading large files using scp_upload() causes RStudio session crash
- Unable to connect from windows due to algorithm mismatch HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ssh.