16

Is it possible to connect to remote ssh server with username and password and read a file? I've done some research and did not come accross with any information about this. I'd appreciate any insight.

Ashrith Sheshan
  • 654
  • 4
  • 17
user1471980
  • 10,127
  • 48
  • 136
  • 235
  • possible duplicate of [Can R read from a file through an ssh connection?](http://stackoverflow.com/questions/2226867/can-r-read-from-a-file-through-an-ssh-connection) – Drew Steen Jun 27 '13 at 15:47

3 Answers3

13

There is direct support for ssh/scp in RCurl:

x = scp("remote.ssh.host.com", "/home/dir/file.txt", "My.SCP.Passphrase", user="username")
Alex Vorobiev
  • 4,349
  • 21
  • 29
5

What is wrong with the answer of @JamesThompson in Can R read from a file through an ssh connection? ? (the second code example works with username and password)

Try the following:

> d <- read.table(pipe('ssh -l user remotehost "cat /path/to/your/file"'))
user@remotehost's password: # type password here

ssh has to be installed and in $PATH.

Community
  • 1
  • 1
sgibb
  • 25,396
  • 3
  • 68
  • 74
  • Maybe I don't understand your problem. After inserting `d <- read.table(pipe('ssh -l user remotehost "cat /path/to/your/file"'))` [ENTER], you will prompt for the password (`user@remotehost's password:`). – sgibb Jun 27 '13 at 16:09
  • I enter the exact line (with my username and hostname), I get this error "no lines available in input". – user1471980 Jun 27 '13 at 16:12
  • Does `ssh -l user remotehost "cat /path/to/your/file"` works in your shell? – sgibb Jun 27 '13 at 16:17
  • I am on a windows servers, I dont have an ssh – user1471980 Jun 27 '13 at 16:19
  • Sorry but the proposed solutions is based on the `ssh` command. You have to install it or better/easier use the solution posted by @AlexVorobiev. – sgibb Jun 27 '13 at 16:22
4

This might not answer @user1471980's initial question but if you are a mac user and could run

ssh -l user remotehost "cat /path/to/your/file"     

in your shell as @sgibb suggested but got the error

ssh_askpass: exec(/usr/libexec/ssh-askpass): No such file or directory     

when you tried running the answer (read.table(pipe('ssh ...'))) in R and did not get a password prompt you probably don't have the script ssh-askpass as the error suggests and need to add/install it.

I didn't know how to do this myself but https://github.com/markcarver/mac-ssh-askpass solved it for me. Here is the script. The github page has instructions on how to install it.

#!/bin/bash
# Script: ssh-askpass
# Author: Mark Carver
# Created: 2011-09-14
# Licensed under GPL 3.0

# A ssh-askpass command for Mac OS X
# Based from author: Joseph Mocker, Sun Microsystems  
# http://blogs.oracle.com/mock/entry/and_now_chicken_of_the

# To use this script:
#   Install this script running INSTALL as root
#
# If you plan on manually installing this script, please note that you will have
# to set the following variable for SSH to recognize where the script is located:
#   export SSH_ASKPASS="/path/to/ssh-askpass"

TITLE="${SSH_ASKPASS_TITLE:-SSH}";
TEXT="$(whoami)'s password:";
IFS=$(printf "\n");
CODE=("on GetCurrentApp()");
CODE=(${CODE[*]} "tell application \"System Events\" to get short name of first process whose frontmost is true");
CODE=(${CODE[*]} "end GetCurrentApp");
CODE=(${CODE[*]} "tell application GetCurrentApp()");
CODE=(${CODE[*]} "activate");
CODE=(${CODE[*]} "display dialog \"${@:-$TEXT}\" default answer \"\" with title \"${TITLE}\" with icon caution with hidden answer");
CODE=(${CODE[*]} "text returned of result");
CODE=(${CODE[*]} "end tell");
SCRIPT="/usr/bin/osascript"
for LINE in ${CODE[*]}; do
SCRIPT="${SCRIPT} -e $(printf "%q" "${LINE}")";
done;
eval "${SCRIPT}";  
zenkavi
  • 41
  • 4
  • 1
    When posting a link always try to include some code or more explanation to avoid dead links. – Leandro Carracedo Jan 09 '15 at 21:58
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Sascha Wolf Jan 10 '15 at 00:23
  • Apologies for the earlier errors. It was my first answer. Just edited to make it more helpful. Hope this helps. Thanks for the suggestions @Zeeker – zenkavi Jan 10 '15 at 00:48
  • for a Ubuntu user, just need to install package *ssh-askpass*: `sudo apt-get install ssh-askpass` – Matifou Aug 21 '17 at 22:41