1

im trying to Disable Wifi, set socks proxy:ip,port,login:pass and then reenable wifi. Im trying to perform this with

NSTask *task = [NSTask new];

[task setLaunchPath:@"/bin/sh"];

[task setArguments:@[@"-c", @"networksetup -setwebproxystate Wi-Fi off"]];

[task launch];

But im dont know exactly how to pass multiple arguments - to set socks proxy and i dont know how to disable and enable wifi, code above dosent work, and last problem is how can i set system password to do not be asked about administrator privileges to change this.

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/networksetup.8.html

1 Answers1

0

Passing multiple arguments to NSTask is a troublesome when up-scaling, for example, passing the below to NSTask with quotes and backslash's to escape the special characters, will fail.

networksetup -getsecurewebproxy Wi-Fi | awk -F ': ' '{ print $2 }' && networksetup -getwebproxy Wi-Fi | awk -F ': ' '{ print $2 }

however if you add the code into a shell script, and run the shell script with NSTask you will not have a problem.

.h file

@property (nonatomic, strong) __block NSTask *buildTask;
@property (nonatomic, strong) NSPipe *outputPipe;


.m file

NSMutableArray *arguments = [[NSMutableArray alloc] init];
[arguments addObject:@"Wi-FI"];
[arguments addObject:@"off"];


//location of script to run
NSString * locationOfScript = [[NSBundle mainBundle] pathForResource:@"wifi" ofType:@"sh"];

    self.buildTask            = [[NSTask alloc] init];
    self.buildTask.launchPath = locationOfScript;       //location of wifi script to run
    self.buildTask.arguments  = arguments;

    // Output Handling
    self.outputPipe               = [[NSPipe alloc] init];
    self.buildTask.standardOutput = self.outputPipe;


    [[self.outputPipe fileHandleForReading] waitForDataInBackgroundAndNotify];
    [self.buildTask launch];
    [self.buildTask waitUntilExit];

  if ([self ->_buildTask terminationStatus] == 0) {
  //Confirms NSTask completes correctly.
  }

Create a new file in xcode project add it you your project with the following.

#!/bin/sh
networksetup -setwebproxystate Wi-Fi off

you will need to use the terminal to chmod +x wifi.sh, otherwise you will receive an error (file not found)

The NSMutableArray's arguments will be passed to the shell script in order and can be accessed with "${1}" being @"Wi-Fi" in the example above.

In regards to your other question, you cannot bypass the requirement for the Administrator password. you can add the command to the sudoers file and the password will not be asked, however this method is not practical for production apps.

with your Socks question, the easiest method would be to create a new file in project name it socks.sh and you can you either use; [-setsocksfirewallproxy networkservice domain portnumber authenticated username password] [-setsocksfirewallproxystate networkservice on | off]

For more info about networksetup and available options can be found in the networksetup man page (linked below) [link]https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/networksetup.8.html

LawrencePires
  • 88
  • 1
  • 5
  • Thank you, will check this. But how to set socks proxy? i Didnt found any helpful in library. –  Sep 06 '14 at 14:41
  • you would create a new script socks.sh and use either,[-setsocksfirewallproxy networkservice domain portnumber authenticated username password] or [-setsocksfirewallproxystate networkservice on | off] more info can be found [link]https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/networksetup.8.html[/link] – LawrencePires Sep 06 '14 at 14:57
  • What's troublesome about passing multiple arguments via `NSTask`? You just set the argument array to the arguments separated at spaces. Don't use `/bin/sh` or its `-c` option. There's little reason to get the shell involved. – Ken Thomases Sep 06 '14 at 17:11
  • Following your method above, NSTask would always fail is you try the below; networksetup -getsecurewebproxy Wi-Fi | awk -F ': ' '{ print $2 }' && networksetup -getwebproxy Wi-Fi | awk -F ': ' '{ print $2 }' – LawrencePires Sep 06 '14 at 19:06
  • Which nobody was trying to do. The way to do the piping is to chain multiple `NSTask`s together with the pipe of one's output being the same as the another's input. The way to do the `&&` is to just check the result of one task before starting the next. Anyway, for *getting* proxy settings, one should use [`CFProxySupport`](https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFProxySupport/Reference/reference.html) and avoid `NSTask` entirely. – Ken Thomases Sep 06 '14 at 19:21
  • You are right, CFProxySupport will do what the OP wanted, – LawrencePires Sep 06 '14 at 19:54
  • `CFProxySupport` can read the proxy settings, but it can't write them. Anyway, I don't mean to argue. I simply was saying that for a simple command, putting the arguments in an array for `NSTask` shouldn't be complicated. I mean, the OP already did that. It's just that he was passing arguments for/to `/bin/sh`. He could just as easily have invoked `networksetup` as the launch path and passed it the arguments in just the same way. – Ken Thomases Sep 06 '14 at 21:30