Open SSH tunnel via Plink and run R Scripts via co

2019-06-06 08:37发布

问题:

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