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

如何在R中使用ssh连接到远程服务器,r,ssh,R,Ssh,是否可以使用用户名和密码连接到远程ssh服务器并读取文件?我做了一些研究,没有得到任何有关这方面的信息。我非常感谢您的任何见解。RCurl中直接支持ssh/scp: x = scp("remote.ssh.host.com", "/home/dir/file.txt", "My.SCP.Passphrase", user="username") 中的@JamesThompson的答案有什么问题? (第二个代码示例使用用户名和密码) 请尝试以下操作: > d <- read.tabl

是否可以使用用户名和密码连接到远程ssh服务器并读取文件?我做了一些研究,没有得到任何有关这方面的信息。我非常感谢您的任何见解。

RCurl中直接支持ssh/scp:

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

中的@JamesThompson的答案有什么问题? (第二个代码示例使用用户名和密码)

请尝试以下操作:

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

>d这可能无法回答@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中运行answer(read.table(pipe('ssh…'))时,没有收到密码提示,您可能没有错误提示的脚本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}";  

也许我不明白你的问题。在插入
d之后,我输入了准确的行(使用我的用户名和主机名),我得到这个错误“输入中没有可用的行”。
ssh-l user remotehost“cat/path/to/your/file”
在您的shell中工作吗?我在windows服务器上,我没有sshSorry,但建议的解决方案是基于
ssh
命令的。您必须安装它,或者更好/更容易地使用@AlexVorobiev发布的解决方案。发布链接时,始终尝试包含一些代码或更多解释,以避免死链接。虽然此链接可能会回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能会无效。对于先前的错误,我深表歉意。这是我的第一个答案。只是为了让它更有用而编辑。希望这有帮助。感谢@zeekerf为Ubuntu用户提供的建议,只需安装ssh-askpass:
sudo-apt-get-install-ssh-askpass