使用 sudo 时使用对话框窗口提示用户输入密码 [英] Prompt user for password with dialog window when using sudo

查看:47
本文介绍了使用 sudo 时使用对话框窗口提示用户输入密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 sudo 执行命令,并希望显示一个对话框窗口供用户输入其凭据.使用 Applescript 自定义提示的尝试非常痛苦,并且使用内置的以管理员权限执行 shell 脚本"不允许自定义窗口,因此用户知道请求来自何处.

I need to execute a command with sudo and want to display a dialog window for the user to enter their credentials. Attempts to customize a prompt with Applescript have been nothing short of excruciating and using the built in "do shell script with with administrator privileges" doesn't allow for customizing the window so the user knows where the request is coming from.

当然,有一种方法可以显示一个窗口,让用户输入他们的凭据并将值发送回 sudo 以执行命令?cocoasudo看起来很有希望,但它也在提示窗口中写入cocoasudo",我需要将其替换为我的应用程序的名称.有没有人找到实现这种功能的解决方案?

Surely, there is a way to display a window, have the user enter their credentials and send the values back to sudo to execute the command? cocoasudo looks promising but it also writes "cocoasudo" in the prompt window which I need to replace with the name of my application. Has anyone found a solution for implementing this kind of functionality?

推荐答案

构建自定义窗口超出了基本的 applescript 的范围.如果需要,您将需要扩展您的编程技能.您需要学习如何利用 Apple 提供的工具来创建窗口等.最重要的是,您需要学习 AppleScriptObjC 或 Objective-C/Cocoa API 以及如何在 Xcode 中使用它们.

Building custom windows is beyond the scope of basic applescript. You will need to expand your programming skills if you want this. You'll need to learn how to leverage to tools that Apple supplies for creating windows and such. Bottom line is you'll need to learn either AppleScriptObjC or Objective-C/Cocoa APIs and how to use them in Xcode.

话虽如此,如果您不喜欢学习新东西,请使用您拥有的工具.像这样的事情会起作用.您可以自定义图标、按钮、文本等.您甚至可以在输入密码时使用隐藏答案来保护用户.无需进一步学习,您可以使用 Applescript 做很多事情...

With that said, if you're not into learning new stuff then use the tools you have. Something like this will work. You can customize the icon, the buttons, the text etc. you can even have a hidden answer to protect the user when entering passwords. There's lots you can do with Applescript without further learning...

display dialog "OSAScript will need an Admin User name and password in order to make your changes." & return & return & "Please enter an admin username." default answer "" with icon 2
set username to text returned of result

display dialog "OSAScript will need an Admin User name and password in order to make your changes." & return & return & "Please enter an admin password." default answer "" with icon 2 with hidden answer
set pssword to text returned of result

do shell script "osascript -e \"return 1\"" user name username password pssword with administrator privileges

这篇关于使用 sudo 时使用对话框窗口提示用户输入密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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