[R] SSH Through R Script
chuck at sharpsteen.net
Tue Apr 13 03:42:27 CEST 2010
> 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?
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:
You can then add your key to the agent:
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
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 )
 "Pseudo-terminal will not be allocated because stdin is not a
Remember to run close() on sshIn when you want to sever the connection.
Hope this helps!
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