如何使用R中的ssh连接到远程服务器

时间:2013-06-27 15:38:46

标签: r ssh

是否可以使用用户名和密码连接到远程ssh服务器并读取文件?我做了一些研究,并没有得到任何关于此的信息。我很欣赏任何见解。

3 个答案:

答案 0 :(得分:12)

在RCurl中直接支持ssh / scp:

x = scp("remote.ssh.host.com", "/home/dir/file.txt", "My.SCP.Passphrase", user="username")

答案 1 :(得分:4)

Can R read from a file through an ssh connection?中@JamesThompson的回答有什么问题? (第二个代码示例使用用户名和密码)

尝试以下方法:

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

ssh必须安装在$PATH

答案 2 :(得分:2)

这可能无法解答@ user1471980的初始问题,但如果您是mac用户并且可以运行

ssh -l user remotehost "cat /path/to/your/file"     
在@sgibb建议的shell中

但是得到了错误

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

当您尝试在R中运行答案(read.table(pipe('ssh ...')))并且没有得到密码提示时,您可能没有脚本ssh-askpass,因为错误建议并且需要添加/安装它。

我自己不知道怎么做,但https://github.com/markcarver/mac-ssh-askpass为我解决了这个问题。这是脚本。 github页面有关于如何安装它的说明。

#!/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}";