Open SSH tunnel via Plink and run R Scripts via command line batch file

♀尐吖头ヾ 提交于 2020-01-14 05:11:11

问题


I am trying to open an SSH tunnel via Plink in the command line to run a few R scripts and I cannot get the R scripts to run and when they do run the tunnel was not created, so the connection to my database crashes.

The Plink code looks like this:

plink.exe -ssh [username]@[hostname] -L 3307:127.0.0.1:3306 -i "[SSH key]" -N

Followed by the code to run the R scripts

"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R

I cannot seem to get the tunnel to stay open to run the R script. However, I can open the tunnel separately and run the code.


回答1:


I assume you have the two commands in a batch-file one after another like:

plink.exe -ssh [username]@[hostname] -L 3307:127.0.0.1:3306 -i "[SSH key]" -N
"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R

Then indeed the R.exe is never executed as the plink.exe runs indefinitely.


You have to run the commands in parallel:

  • You can use start command to run plink.exe in the background.
  • Use killtask command to kill the background plink process after the R.exe finishes.
  • You should probably also pause a little before you run R.exe to allow the Plink to establish the tunnel.
rem Open tunnel in the background
start plink.exe -ssh [username]@[hostname] -L 3307:127.0.0.1:3306 -i "[SSH key]" -N

rem Wait a second to let Plink establish the tunnel 
timeout /t 1

rem Run the task using the tunnel
"C:\Program Files\R\R-3.2.1\bin\x64\R.exe" CMD BATCH qidash.R

rem Kill the tunnel
taskkill /im plink.exe


来源:https://stackoverflow.com/questions/32193256/open-ssh-tunnel-via-plink-and-run-r-scripts-via-command-line-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!