SSH Through R Script

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

SSH Through R Script

afoo
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
Reply | Threaded
Open this post in threaded view
|

Re: SSH Through R Script

Jonathan Baron
You might try setting up ssh so that you do not need a password.  See
man ssh-keygen

In essence, you make a key for the machine you are on with (for
example):

ssh -t dsa

which produces a public and a private key.  You upload the public key
to remoteserver.com, and put it in your .ssh directory by adding it to
(or creating) a file called authorized_keys, e.g.

cat id_dsa.pub >> .ssh/authorized_keys

When you are asked for a passphrase, leave it blank.

Then you don't need to enter your username or password in order to
connect.

This assumes that remoteserver.com runes linux or unix.  If not, I
have no idea what to do.

My impression is that this method is no less secure on the whole than
using passwords.  (I do it with the full knowledge of our
security-obsessed computing staff.)

Jon

On 04/08/10 22:01, 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
> --
> View this message in context:
> http://n4.nabble.com/SSH-Through-R-Script-tp1809635p1809635.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--
Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page: http://www.sas.upenn.edu/~baron
Editor: Judgment and Decision Making (http://journal.sjdm.org)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: SSH Through R Script

Don MacQueen
In reply to this post by afoo
When I do what you're describing, I get prompted for my password:

    >  system('ssh -l  usernm rmthost')
    usernm@rmthost's password:

After I enter my password, nothing seems to happen. But if I hit
ctrl-c then I get a command line prompt, and it turns out that it's a
shell prompt on the remote host. I can issue standard unix commands,
and they execute on the remote host. To get back into R on my local
host I type 'exit'.

Or:

Here's a couple of lines from one of my scripts that might help ...

     cmd <- 'ssh -l username remotehost /bin/lpstat -a'
     lprs <- read.table(pipe(cmd),fill=TRUE,as.is=TRUE)[,1]

I get prompted for my password when I source these lines on my local host.

With the result that the lprs object has the names of printers
available on the remote host.

-Don

At 10:01 PM -0800 4/8/10, 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
>--
>View this message in context:
>http://*n4.nabble.com/SSH-Through-R-Script-tp1809635p1809635.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] mailing list
>https://*stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://*www.*R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.


--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: SSH Through R Script

Sharpie
In reply to this post by afoo
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:

  ssh-keygen
  Enter file in which to save the key (/Users/Sharpie/.ssh/id_rsa):  /Users/Sharpie/.ssh/webKey
  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@webserver.com:~/.ssh
  ssh user@webserver.com -e "cd ~/.ssh;cat webKey.pub >> authorized_keys"

You could now sign in using the key:

  ssh -i ~/.ssh/webKey user@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@server.com &> sshOut', open = 'w'
  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 terminal."
 [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
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
Reply | Threaded
Open this post in threaded view
|

Re: SSH Through R Script

Sharpie
Sorry about the double post, but this line:

Sharpie wrote
  ssh user@webserver.com -e "cd ~/.ssh;cat webKey.pub >> authorized_keys"
Should be:

  ssh user@webserver.com "cd ~/.ssh;cat webKey.pub >> authorized_keys"

I.e., omit the -e flag.

Apologies,

-Charlie
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
Reply | Threaded
Open this post in threaded view
|

Re: SSH Through R Script

Jonathan Baron
Another way to solve the problem is to mount the remote server using
sshfs and then access it as a directory on your local computer.  This
will require using the password once when you mount the server, but
you can just leave it mounted until you shut down your computer.
E.g.,

You have an empty directory called remoteserver.

sshfs [hidden email]: remoteserver

To unmount, say
fusermount -u remoteserver
but shutting down the computer will also do this.

Jon

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.