Prompt user for password with dialog window when using sudo

筅森魡賤 提交于 2019-12-02 08:51:19

问题


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.

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?


回答1:


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.

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



回答2:


In addition, consider that once you authenticate with sudo, you don't need to provide a password again for the next five minutes. That doesn't directly solve your problem, but it gives you more options for the way you interact with the user. The -n option to sudo prevents it from prompting for a password (the command runs, or sudo quits with an error).

You could use -n in combination with the -A option, which causes sudo to run a separate program whose only job is to collect and output a password. Would that let you use AppleScript to better do what you want?




回答3:


If you are familiar with Javascript, you can use the sudo-prompt module for Node to run your command using sudo and to prompt with a dialog window if necessary. The dialog is a native prompt raised by the operating system so that the user's password is never exposed to your application. You can also provide the name and icon of your application on macOS. The module also works on Windows and Linux.



来源:https://stackoverflow.com/questions/7059655/prompt-user-for-password-with-dialog-window-when-using-sudo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!