[R] SSH Through R Script

Sharpie chuck at sharpsteen.net
Tue Apr 13 03:42:27 CEST 2010

afoo wrote:
> Hi,
> I am trying to SSH to a remote server through R script. In other words, I
> would like to know how I can get a SSH connection to the remote server and
> then execute commands on that server with the R script.
> So in bash, I would normally type ssh -lusername remoteserver.com; press
> enter and then wait for the password prompt to key in my password.
> I have tried system("ssh remoteserver.com") but that doesn't work because,
> from what I know, SSH requires user interactivity - I am required to key
> in my password.
> I tried looking up about putting password as a command line parameter, but
> SSH doesn't allow that, my only option then is to set up a private/public
> key pair. But the admin of the remoteserver doesn't allow me to do that.
> Is there a way in which I can SSH in? Or is there a command in R that
> allows me to interact with the command prompts interactively?
> thanks,
> afoo

You can secure your public/private keys with a password if there is a
concern about security.  You can still use these keys to allow programs
automatic access by using ssh-agent to hold the identities.  For example,  I
could create a new key for my web server like so:

  Enter file in which to save the key (/Users/Sharpie/.ssh/id_rsa): 
  Enter passphrase (empty for no passphrase): superSecretPassword
  Enter same passphrase again: superSecretPassword

The next step is to copy the public key, ~/.ssh/webKey.pub, to the remote
server and add it to the authorized keys file.

  scp ~/.ssh/webKey.pub user at webserver.com:~/.ssh
  ssh user at webserver.com -e "cd ~/.ssh;cat webKey.pub >> authorized_keys"

You could now sign in using the key:

  ssh -i ~/.ssh/webKey user at webserver.com

But you still have to provide a password since the key is protected.  To
ease this restriction, start ssh-agent.  ssh-agent provides some environment
variables that must be set, so it has to be run using eval ` `, like so:

  eval `ssh-agent`

You can then add your key to the agent:

  ssh-add ~/.ssh/webKey

You still have to enter your password, but from now on all processes spawned
from this shell can use that key without requiring the password to be
re-entered.  Now you can start R and access ssh key free.

The best way I can think of to run ssh from within R is to start the process
on a pipe and have it write the output to a fifo.  You can then use
writeLines to send commands to the pipe and readLines to get the output from
the fifo.  First, make the fifo

  system('mkfifo sshOut')

Then, connect to the pipe and the fifo from within R:

  # The &> redirects both stdout and stderr to the fifo
  sshIn <- pipe( 'ssh -i ~/.ssh/webKey user at server.com &> sshOut', open =
  sshOut <- fifo( 'sshOut', 'r' )

Now you can queue commands to be executed with writeLines(), send them with
flush() and get the results using readLines():

  writeLines( 'ls', sshIn )
  flush( sshIn )

  readLines( sshOut )

 [1] "Pseudo-terminal will not be allocated because stdin is not a
 [2] "backup"                                                                
 [3] "bin"                                                                   
 [4] "cache"                                                                 
 [5] "Cellar"                                                                
 [6] "code"                                                                  
 [7] "dat"                                                                   
 [8] "doc"                                                                   
 [9] "gems"                                                                  
[10] "gitrepos"                                                              
[11] "include"                                                               
[12] "lib"                                                                   
[13] "libexec"                                                               
[14] "logs"                                                                  
[15] "man"                                                                   
[16] "opt"                                                                   
[17] "share"                                                                 
[18] "source"                                                                
[19] "specifications"                                                        
[20] "stash"                                                                 
[21] "webapps" 

Remember to run close() on sshIn when you want to sever the connection.

Hope this helps!


Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
View this message in context: http://n4.nabble.com/SSH-Through-R-Script-tp1809635p1837919.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list