我怎样才能在一个Capistrano的任务切换用户?(How can I switch user i

2019-08-01 04:23发布

我做了下面一个小的测试任务:

set :user, "user"
set :password, "password"
set :root_password, "root password"
set :use_sudo, false
role :srv, "exmaple.com"

task :show_info do
    run "iptables -L", :shell => "su -" do |channel, stream, data|
        channel.send_data("#{root_password}\n")
    end
end

该服务器不允许我使用sudo ,所以我必须要登录,普通用户则成为root。

我也试图创造surun从这篇文章 ,虽然它并不能帮助我... :(

可能有人请告诉我有前途的方法Capistrano的切换到root后运行命令?

提前致谢。

PS新增输出日志:

$ cap show_info
  * executing `show_info'
  * executing "iptables -L"
    servers: ["example.com"]
    [example.com] executing command
    command finished in 000ms
failed: "su - -c 'iptables -L'" on example.com

Answer 1:

点是default_run_options [:PTY] =真

试试这个

def surun(command)
  default_run_options[:pty] = true
  password = fetch(:root_password, Capistrano::CLI.password_prompt("root password: "))
  run("su - -c #{command}") do |channel, stream, output|
      channel.send_data("#{password}\n")
  end
end


文章来源: How can I switch user in a Capistrano task?
标签: capistrano su