use*_*905 7 macos cocoa nstask
我试图弄清楚如何在提示时将输入传递给NSTask.
例:
我做的事情
kinit username@DOMAIN
Run Code Online (Sandbox Code Playgroud)
然后我收到"输入密码"提示.我希望能够为该NSTask提供密码.
有谁知道如何做到这一点?(基本上通过可可应用程序自动化过程).
谢谢!
小智 3
通常,命令行应用程序通过标准输入从命令行读取输入。setStandardInput:
NSTask 提供了设置 aNSFileHandle
或 a 的方法NSPipe
。
你可以尝试这样的事情:
NSTask *task = // Configure your task
NSPipe *inPipe = [NSPipe pipe];
[task setStandardInput:inPipe];
NSPipe *outPipe = [NSPipe pipe];
[task setStandardOutput:outPipe];
NSFileHandle *writer = [inPipe fileHandleForWriting];
NSFileHandle *reader = [outPipe fileHandleForReading];
[task launch]
//Wait for the password prompt on reader [1]
NSData *passwordData = //get data from NSString or NSFile etc.
[writer writeData:passwordData];
Run Code Online (Sandbox Code Playgroud)
有关等待读取器 NSFileHandle 上的数据的方法,请参阅NSFileHandle 。
然而,这只是一个未经测试的示例,展示了使用提示使用命令行工具时解决此问题的一般方法。对于您的具体问题,可能还有另一种解决方案。该kinit
命令允许--password-file=<filename>
使用可用于从任意文件读取密码的参数。
从man kinit
:
--密码文件=文件名
从文件名的第一行读取密码。如果文件名是 STDIN,则将从标准输入读取密码。
该手册提供了第三种解决方案:作为 NSTask 的参数
提供--password-file=STDIN
,并且不会出现密码提示。这简化了通过 NSPipe 提供密码的过程,因此您无需等待标准输出的密码提示。
结论:使用第三种解决方案时要容易得多:
--password-file=STDIN
使用参数配置您的任务