Launching command using NSTask returns error - objective-c

I'd like to launch the following command from my application using NSTask:
sudo -u myusername launchctl load /Library/LaunchAgents/com.google.keystone.agent.plist
Here is a code I do:
NSPipe *pipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: #"/bin/sh"];
[task setCurrentDirectoryPath:#"/"];
[task setStandardError:pipe];
NSArray *arguments = nil;
arguments = #[#"sudo",
#"-u",
#"myusername",
#"launchctl",
#"load",
#"/Library/LaunchAgents/com.google.keystone.agent.plist"];
[task setArguments: arguments];
NSFileHandle * read = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
NSData * dataRead = [read readDataToEndOfFile];
NSString * output = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding];
NSLog(#"output: %#", output);
After processing I receive an error below:
/bin/sh: sudo: No such file or directory

I've found out solution:
arguments = #[#"-c",
#"sudo -u myusername launchctl load /Library/LaunchAgents/com.google.keystone.agent.plist"];

Related

NSTask get the output of a command

I am trying to run an executable on OS X from one of the other application.
NSPipe *pipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
task.arguments = #[#"param1", #"param2", #"param3", #"param4"];
task.launchPath = #"/usr/bin/myApp";
[task setStandardOutput: pipe];
[task launch];
[task waitUntilExit];
NSFileHandle *file = [pipe fileHandleForReading];
NSData *output = [file readDataToEndOfFile];
NSString *outputString = [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding];
[file closeFile];
NSLog(#"%#",outputString);`
But the output is nothing. Though, I am sure NSTask is getting executed. I think I am doing something wrong with NSPipe. But I am still not seeing the expected output.
Thanks.

How to run sudo command programmatically on OS X

I am trying to run sudo command programmatically in my OS X app,
but i got these message
sudo: no tty present and no askpass program specified
Here is my code:
NSString *runCommand(NSString *commandToRun)
{
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: #"/bin/sh"];
NSArray *arguments = [NSArray arrayWithObjects:#"-c" ,[NSString stringWithFormat:#"%#", commandToRun], nil];
NSLog(#"run command: %#",commandToRun);
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *output;
output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
return output;
}
And i call this method like this :
runCommand(#"sudo purge");
Thanks for any help or sugession.

NSTask - execute echo command

I'm trying to run a simple task which has to execute an echo "Hello World"
Well here is my code:
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:#"/bin/bash"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects:#"echo","hello world" nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task setStandardError: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
//...
//Code to get task response
Keep giving me no such file or directory error.. What am I doing wrong ?
The right way to execute a command is
bash -c "echo 'hello world'"
which means the arguments you should pass are
arguments = [NSArray arrayWithObjects:#"-c", #"echo 'hello world'", nil];

executing shell command with | (pipe) using NSTask

I'm trying to execute this comamnd ps -ef | grep test using NSTask but I can't get the | grep test to be included in the NSTask:
This is what I'm using currently to get the output of ps -ef into a string then I need to somehow get the pid of the process test
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: #"/bin/ps"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: #"-ef", nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
NSLog (#"got\n%#", string);
Piping is a feature provided by shells, such as /bin/sh. You may try launching your command via such a shell:
/* ... */
[task setLaunchPath: #"/bin/sh"];
/* ... */
arguments = [NSArray arrayWithObjects: #"-c", #"ps -ef | grep test", nil];
However, if you let the user supply a value (instead of hard-coding e.g. test), you are making the program susceptible to shell injection attacks, which are kind of like SQL injection. An alternative, which doesn't suffer from this problem, is to use a pipe object to connect the standard output of ps with the standard input of grep:
NSTask *psTask = [[NSTask alloc] init];
NSTask *grepTask = [[NSTask alloc] init];
[psTask setLaunchPath: #"/bin/ps"];
[grepTask setLaunchPath: #"/bin/grep"];
[psTask setArguments: [NSArray arrayWithObjects: #"-ef", nil]];
[grepTask setArguments: [NSArray arrayWithObjects: #"test", nil]];
/* ps ==> grep */
NSPipe *pipeBetween = [NSPipe pipe];
[psTask setStandardOutput: pipeBetween];
[grepTask setStandardInput: pipeBetween];
/* grep ==> me */
NSPipe *pipeToMe = [NSPipe pipe];
[grepTask setStandardOutput: pipeToMe];
NSFileHandle *grepOutput = [pipeToMe fileHandleForReading];
[psTask launch];
[grepTask launch];
NSData *data = [grepOutput readDataToEndOfFile];
/* etc. */
This uses built-in Foundation functionality to perform the same steps as the shell does when it encounters the | character.
Finally as others have pointed out, the usage of grep is overkill. Just add this to your code:
NSArray *lines = [string componentsSeparatedByString:#"\n"];
NSArray *filteredLines = [lines filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: #"SELF contains[c] 'test'"]];
You may need to call [task waitUntilExit] before you launch the task, so that the process can finish running before you read the output.

Run terminal command with NSTask

I want to run a Terminal command in my program.
The command looks like this:
cd /path/to/file/; ./foo HTTPProxy 127.0.0.1
It works with system() but it doesn't work when I use NSTask.
system("cd /path/to/file/; ./foo HTTPProxy 127.0.0.1");
works fine but
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:#"/path/to/file/./foo"];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task setArguments:[NSArray arrayWithObjects:#"HTTPProxy 127.0.0.1", nil]];
[task launch];
NSData *data = [file readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(string);
doesn't.
Output:
Command-line option 'HTTPProxy 127.0.0.1' with no value. Failing.
Has anybody an idea?
Now I think I have got it:
[task setArguments:[NSArray arrayWithObjects:#"HTTPProxy", #"127.0.0.1", nil]];
those are separate arguments in your invocation from the command line...
OLD ANSWER:
You could trying setting the current directory for execution:
– setCurrentDirectoryPath:
This is basically the effect of cd in the system version of your code.