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

时间:2020-12-05 20:55:28

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.

是否可以用用户名和密码连接到远程ssh服务器并读取文件?我做了一些调查,没有得到任何关于这个的信息。我感激任何见解。

3 个解决方案

#1


11  

There is direct support for ssh/scp in RCurl:

RCurl支持ssh/scp:

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

#2


3  

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)

@JamesThompson的答案可以通过ssh连接从文件中读取吗?吗?(第二个代码示例使用用户名和密码)

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.

ssh必须安装在$PATH中。

#3


2  

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

这可能不能回答@user1471980的最初问题,但是如果您是mac用户,并且可以运行的话

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

in your shell as @sgibb suggested but got the error

在你的shell中,@sgibb建议但是得到了错误

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.

当你试着运行答案时。在R中没有得到密码提示,您可能没有脚本ssh-askpass(错误提示),需要添加/安装它。

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.

我自己不知道怎么做,但是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}";  

#1


11  

There is direct support for ssh/scp in RCurl:

RCurl支持ssh/scp:

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

#2


3  

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)

@JamesThompson的答案可以通过ssh连接从文件中读取吗?吗?(第二个代码示例使用用户名和密码)

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.

ssh必须安装在$PATH中。

#3


2  

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

这可能不能回答@user1471980的最初问题,但是如果您是mac用户,并且可以运行的话

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

in your shell as @sgibb suggested but got the error

在你的shell中,@sgibb建议但是得到了错误

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.

当你试着运行答案时。在R中没有得到密码提示,您可能没有脚本ssh-askpass(错误提示),需要添加/安装它。

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.

我自己不知道怎么做,但是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}";