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,

jamespick
  • 1,974
  • 3
  • 23
  • 45
  • You can use `NSTask`: [How to use NSTask as root?](http://stackoverflow.com/q/4050687) – jscs Jul 20 '13 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) – jamespick Jul 20 '13 at 18:59
  • This is the error I get when I run STPrivilegedTask – jamespick Jul 20 '13 at 19:00
  • STPrivilegedTask isn't built in to Cocoa. You need to download the files and compile them. – jscs Jul 20 '13 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. – jamespick Jul 20 '13 at 19:03
  • That's a linker error. Make sure STPrivilegedTask.m is correctly added to your build. It needs to be marked as a member of your target in the File Inspector, and be listed in the "Compile Sources" Build Phase. – jscs Jul 20 '13 at 19:06
  • I added it to those places and now it gives me all these reference counting errors. Is there any way I can have automatic reference counting for everything but stprivilegedtask? – jamespick Jul 20 '13 at 19:13

1 Answers1

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;
Chris N
  • 905
  • 5
  • 10