Skip to content

Instantly share code, notes, and snippets.

@iburlakov
Created October 9, 2014 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iburlakov/bf12ddbba474093899f4 to your computer and use it in GitHub Desktop.
Save iburlakov/bf12ddbba474093899f4 to your computer and use it in GitHub Desktop.
Get domain name from dsconfigad
NSString *
getDomainName() {
// "dsconfigad --show" command returns domain which mac joined to or nothing if mac is not joined to any domain
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/sbin/dsconfigad"];
[task setArguments: [NSArray arrayWithObjects:@"--show", nil]];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *file = [pipe fileHandleForReading];
[task launch];
[task waitUntilExit];
NSString *string = [[NSString alloc] initWithData: [file readDataToEndOfFile] encoding: NSUTF8StringEncoding];
NSError *err;
NSRegularExpression *regex = [[NSRegularExpression alloc]
initWithPattern:@"Active Directory Domain\\s+=\\s+(.+)\\s" options:NSRegularExpressionCaseInsensitive error:&err];
if (!err) {
NSTextCheckingResult *match = [regex firstMatchInString:string
options:0
range:NSMakeRange(0, [string length])];
if(match) {
if ([match numberOfRanges] > 1) {
// domain name is first regex group
NSString *domainName = [string substringWithRange:[match rangeAtIndex:1]];
return domainName;
}
}
}
return nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment