首页 > 解决方案 > Run a app from terminal and send a password

问题描述

I want to launch a shiny application from a terminal to avoid blocking the rstudio console.

My application uses the ssh package to connect to a remote machine.

When I launch the application from rstudio a small window opens to ask me for my password but when I launch the application from the terminal i get an error message.

$ "Rscript.exe" -e "shiny::runApp('app')"
Loading required package: shiny
Warning: package 'shiny' was built under R version 3.5.3
Warning: package 'ssh' was built under R version 3.5.3
Linking to libssh v0.8.6
Password callback did not return a string value
Erreur : Authentication with ssh server failed
Stopped

app.R

library(shiny)
library(ssh)

ssh.session <- ssh::ssh_connect(host = host)
cat("*** Logging in of the session ***")

# Define UI for application that draws a histogram
ui <- fluidPage()

server <- function(input, output) {

  onStop(function() {
    ssh_disconnect(ssh.session)
    cat("*** Logging out of the session ***")
  })
}

# Run the application
shinyApp(ui = ui, server = server)

标签: shiny

解决方案


我不知道您在用例中的安全必要性...

您可以做的(如果您可以在调用 Rscript 时指定密码)是在调用时将密码作为额外参数提供RScript,然后将其转发到ssh_connect(). 您也可以对主机变量执行相同的操作。我通过以下方式调用此 R 文件:

Rscript --vanilla home/user/R/app.R "password"

我没有运行shiny::runApp,因为线路shinyApp(ui=ui, server=server)会自动启动闪亮的应用程序,让我们事先连接到 ssh。

在 app.R 文件中,我们将参数转发给 ssh_connect。

应用程序.R

library(shiny)
library(ssh)
args <- commandArgs(trailingOnly = TRUE)
host="host"
ssh.session <- ssh::ssh_connect(host = host,passwd = )
cat("*** Logging in of the session ***")

# Define UI for application that draws a histogram
ui <- fluidPage()

server <- function(input, output) {

  onStop(function() {
    ssh_disconnect(ssh.session)
    cat("*** Logging out of the session ***")
  })
}

# Run the application
shinyApp(ui = ui, server = server)

推荐阅读