15

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

4

3 回答 3

13

RCurl 中直接支持 ssh/scp:

x = scp("remote.ssh.host.com", "/home/dir/file.txt", "My.SCP.Passphrase", user="username")
于 2013-06-27T16:01:55.023 回答
5

@JamesThompson 在Can R 通过 ssh 连接读取文件中的答案有什么问题?? (第二个代码示例使用用户名和密码)

尝试以下操作:

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

ssh必须安装并在$PATH.

于 2013-06-27T16:03:53.857 回答
4

这可能无法回答@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}";  
于 2015-01-09T21:38:04.847 回答