如何在本地用户名下的远程计算机上 运行 R 脚本(它具有使用集成 windows 身份验证的数据库连接)?

How to run an R script (which has database connection using integrated windows authentication) on a remote machine under local username?

考虑以下场景:

我们有以下 R 脚本 (DB.r):

lib.directory = "D:\RTest"
install.packages("RODBC", repos = "http://cran.us.r-project.org", lib = lib.directory)
library(RODBC, lib.loc = lib.directory)

db.string <- "driver={ODBC Driver 13 for SQL Server};server=DBServer;database=Databse1;trusted_connection=Yes;"
db.channel <- odbcDriverConnect(db.string)
close(db.channel)

Server1 使用以下代码在 R Server 上远程执行 R 脚本:

PsExec.exe \RServer "C:\Program Files\R\R-3.4.3\bin\Rscript.exe" "D:\RTest\DB.r"

我收到以下错误:

[RODBC] ERROR: state 28000, code 18456, message [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

我们如何在不将用户名和密码作为 PsExec 的一部分发送的情况下解决此错误?

我们愿意使用任何替代方法来替换 PsExec。

问题不在于您的代码。您看到的是经典 "double-hop" problem. While Server1 knows your identity when you're logged onto your workstation using integrated Windows authentication also known as ,RServer 不知道您的身份,因为从 Server1 传递给它的不是您的身份令牌,而是 Server1(本地系统)的计算机帐户凭据。由于 RServer 可能不允许匿名访问,因此连接失败:Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.

在这种情况下,RServer 基本上是 "Server2",如下面的屏幕截图所示。从您的客户端工作站的角度来看,它是您的第 2 跳。

要完成此工作,您需要在 Server1 上配置 Kerberos 委派,以便它能够将任何身份令牌传递给 RServer,以便连接成功。请注意,此身份令牌不是用户名或密码,而是 Kerberos 票证。您在帐户 运行 上配置配置 Kerberos 委派,该进程将启动从 Server1 到 RServer 的连接。该帐户需要有 . Read through the steps in this article to get a understanding of this problem and how to configure an SPN: Understanding Kerberos Double Hop

进一步参考:

SQL Server returns error “Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.” in Windows application

Web App getting Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'

Permissions for PSExec run from SQL job