0

I am writing a application that can run shell commands remotely. I need to run the command: tc -l 455 as an administrator. I decided to run this command in applescript with administrative privileges but in objective c I need to take this output and display it in a NSTexView. Here is my code:

NSAppleScript* runWithAdminPrivileges = [[NSAppleScript alloc] initWithSource:@"do     shell script \"nc -l 455\" with administrator privileges"];
NSDictionary *error = [[NSDictionary alloc] init];
[runWithAdminPrivileges executeAndReturnError:&error];
NSLog(@"%@", error);

The command is running but I have no way of seeing the output. Is there a way to do this with my code or is there a way to run this shell command in objective c with admin privileges and view the output?

Thanks in advance,

7
  • You can use NSTask: How to use NSTask as root? Commented Jul 20, 2013 at 18:49
  • Undefined symbols for architecture x86_64: "_OBJC_CLASS_$_STPrivilegedTask", referenced from: objc-class-ref in AppDelegate.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) Commented Jul 20, 2013 at 18:59
  • This is the error I get when I run STPrivilegedTask Commented Jul 20, 2013 at 19:00
  • STPrivilegedTask isn't built in to Cocoa. You need to download the files and compile them. Commented Jul 20, 2013 at 19:01
  • I did download them and imported them into my project. In the source code there are no errors. It is only this runtime error. Commented Jul 20, 2013 at 19:03

1 Answer 1

1

-[NSAppleScript executeAndReturnError:] returns a result you’re ignoring, which is the result of the script. It’s an NSAppleEventDescriptor, not an NSString, but you can get an NSString by calling -stringValue on it. Also, the error parameter follows the same rules as NSError ** parameters elsewhere, so you don’t need to fill it in with an object first. To sum up:

NSDictionary *errorInfo; // no initialization necessary.
NSAppleEventDescriptor *scriptResult = [runWithAdminPrivileges executeAndReturnError:&error];
if (scriptResult)
    NSLog(@“%@“, [scriptResult stringValue]);
else
    NSLog(@“%@“, errorInfo;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.