Typically, command line applications read input from the command line through standard input. NSTask provides the setStandardInput: method for setting NSFileHandle or NSPipe .
You can try something like:
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];
See NSFileHandle for data wait methods on an NSFileHandle reader.
However, this is just an unverified example, showing a general way to solve this problem using command-line tools using tooltips. There may be a different solution for your specific problem. The kinit command allows you to use the --password-file=<filename> argument, which you can use to read the password from an arbitrary file.
From man kinit :
- password file = file name
read the password from the first line of the file name. If the file name is STDIN, the password will be read from standard input.
The manual contains the third solution: Provide --password-file=STDIN as an argument for your NSTask and there will be no password prompt. This simplifies the process of providing a password through NSPipe, so you do not need to wait for standard output for a password prompt.
Conclusion: when using the third solution, this is much simpler:
- Configure the task using the
--password-file=STDIN option - Create NSPipe
- Use it as standard input for your task.
- run task
- write password data to the channel through [pipe fileHandleForWriting] (NSFileHandle)
Tobiasmende
source share