Ruby CLI:提示输入 root 密码并以 root 身份继续执行脚本? [英] Ruby CLI: Prompt for root password and continue executing script as root?

查看:34
本文介绍了Ruby CLI:提示输入 root 密码并以 root 身份继续执行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了要求用户通过 sudo 调用 ruby​​ 脚本之外,还有没有办法以普通用户的身份开始运行脚本,然后在运行时的某个时间点将权限提升到 root,通过提示用户输入他们的 root 密码?

Other than requiring the user to invoke a ruby script through sudo, is there a way to start running a script as a regular user, then at some point during runtime, elevate privileges to root, by prompting the user for their root password?

也许使用以 sudo 为前缀的当前命令类似于 #exec 的内容?

Maybe something along the lines of #exec using the current command prefixed with sudo?

推荐答案

这有效.它只是使用 exec 再次调用自己(在本例中为 test_script ).但是要非常小心,通过添加一个调用 exit 的条件来确保它不会无限运行.

This works. It just uses exec to call itself (test_script in this case) again. But be very careful to make sure that it doesn't run infinitely by adding a condition which will call exit.

#!/usr/bin/env ruby

if ARGV[0] == "--second"
  puts "...called again and exiting."
  exit
end

puts "Calling self again..."
exec "sudo ./test_script --second"

这篇关于Ruby CLI:提示输入 root 密码并以 root 身份继续执行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆