Giter Club home page Giter Club logo

Comments (6)

jeroen avatar jeroen commented on May 28, 2024 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.

PaulC91 avatar PaulC91 commented on May 28, 2024 1

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.

vmorgan avatar vmorgan commented on May 28, 2024

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.

djbirke avatar djbirke commented on May 28, 2024

@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.

jeroen avatar jeroen commented on May 28, 2024

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.

djbirke avatar djbirke commented on May 28, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.