我需要在R中连接到使用SSH隧道的DB。基于googling,所以告诉我我是必须首先创建一个SSH隧道,然后连接到我的DB,但是在执行我的SQL之后,如何安全地关闭隧道几乎没有什么意义。
示例:
if(!require(RPostgreSQL)) install.packages("RPostgreSQL")
# Create tunnel
myssh<-"ssh -f <server_user>@<server_ip> -L <unused_local_port>:localhost:<database_remote_port> -N"
system(myssh)
# Connect to DB
drv <- dbDriver("PostgreSQL")
conn <- dbConnect(
drv,
host = "127.0.0.1", user = "postgres",
password = "", dbname = "mydb",
)
# How to close tunnel?如何正确检测和关闭R?创建的SSH隧道
PS我在Windows上!
发布于 2016-02-24 13:39:25
我在自动封闭SSH隧道上找到了一篇很好的文章--它建议使用这样的代码来连接:
ssh -f -L 3306:127.0.0.1:3306 me@remote.example.org sleep 10;它使用的特性是ssh在使用时不会关闭连接。它将首先等待10s -如果连接被使用,则在连接完成后关闭该连接。
发布于 2016-02-24 12:05:38
在Mac和Linux上,您可以像其他任何背景ssh连接一样关闭它:
ps -ef | grep ssh然后
kill -9 pid我不知道窗户上有没有ps
https://stackoverflow.com/questions/35601426
复制相似问题