rstudio / rstudioapi

Safely access RStudio's API (when available)
http://rstudio.github.io/rstudioapi
Other
165 stars 35 forks source link

SSH Tunnel Persists when killing terminal #255

Closed privatedeal2017 closed 2 years ago

privatedeal2017 commented 2 years ago

Hello, I'm using rstudioapi to open an ssh tunnel in the RStudio Terminal. This allows me to directly run scripts that require a connection to my company database without separately opening the windows command prompt and opening the ssh tunnel there.

The problem is that if I kill the terminal in which the tunnel is open, it somehow does not close the connection. That means I can still connect to the database and pull tables even though there's technically no open terminal with the ssh tunnel running. In windows, if I close the terminal prompt containing the ssh tunnel, the tunnel is closed.

I read the package documentation, and I can't find a way to send the 'exit' command to an existing terminal and run it. The terminalSend() and terminalExecute() don't work together. The former only "sends" the command, where the latter always send the command to a new terminal. Do you know how to either kill the terminal and the ssh terminal simultaneously, or send and execute a command in the existing terminal?

kevinushey commented 2 years ago

Just to confirm, by "kill" do you mean using rstudioapi::terminalKill()?

If you create your terminal using rstudioapi, you should be able to save its ID, and use that for sending and executing terminal commands. For example:

id <- rstudioapi::terminalCreate("My Terminal")
rstudioapi::terminalSend(id, text = "exit\n")

You could also use Tools -> Terminal -> Terminal Diagnostics... to figure out the handles (ids) for your existing terminals, and use those for communication via rstudioapi. Or, if you just have a single terminal, use rstudioapi::terminalList() to get its information.

Let me know if any of this helps.

privatedeal2017 commented 2 years ago

Thank you for the reply @kevinushey !

I have to admit I'm a bit lost with the syntax.

If I use: id <- rstudioapi::terminalExecute('mysshtunnelcredentials') with my ssh tunnel credentials, it opens up the tunnel. But then if I run the command you gave above: rstudioapi::terminalSend(id, text = "exit\n") it doesn't execute the "exit" line, it simply types it in.

Now, if I do what you suggested above: id <- rstudioapi::terminalCreate("My Terminal") then rstudioapi::terminalSend(id, text = 'mysshtunnelcredentials\n') it only types the tunnel credentials in and I still have to manually go to the terminal and press enter for it to execute. But then if I do: rstudioapi::terminalSend(id, text = "exit\n") it correctly types in "exit" and executes, closing the tunnel. If I then kill the terminal using: rstudioapi::terminalKill(id) it kills the terminal and the tunnel is closed.

I'm not sure why the "exit\n" line only works with method 2 and not method 1. Alternatively, is there a way I can get the ssh tunnel credentials to execute in terminalSend() by adding "\n"? It currently doesn't work. The format is as follows: 'ssh -i ".ssh\\mykey" -L myport:databaseurl:myport user@userip' If I create a terminal, and then use the terminalSend() to send the key, it doesn't execute. If I add: 'ssh -i ".ssh\\mykey" -L myport:databaseurl:myport user@userip\n', it also doesn't execute. Any idea why?

I also still don't quite understand how killing the terminal the tunnel was open in doesn't result in the tunnel being closed same as it is in Windows.

kevinushey commented 2 years ago

Interesting; thanks! It's strange that sending the \n doesn't always work for you; it seems to consistently work for me. Can you share any other details (e.g. your operating system, version of RStudio, what shell you're using)? The output in Tools -> Terminal -> Terminal Diagnostics... may also be useful (run that after creating a terminal, but before closing it).

Does it make any difference if you sent multiple newline characters, e.g. \n\n? (Maybe something else is prompting for input and eating the initial newline character?)

privatedeal2017 commented 2 years ago

Okay so if I use a Git Bash instead of the cmd/windows powershell, it works.

I have windows 11, R Studio 2022.02.0 build 443 and right now I switched to Git Bash as the shell.

I'll experiment some more this evening, but it is a little puzzling. That being said, thank you for all the help. I have a functional script now!

kevinushey commented 2 years ago

Thanks! I think I know what the issue is -- for cmd.exe (and probably Powershell), you likely need to insert \r\n rather than just \n to ensure the command is submitted. So, I would expect the following to work with a command shell:

id <- rstudioapi::terminalCreate("My Terminal")
rstudioapi::terminalSend(id, text = "exit\r\n")
privatedeal2017 commented 2 years ago

Sorry about the last response, but I tried it out and it works with the cmd/windows powershell as well now! Thank you again for all the help!