Created
October 9, 2014 11:44
-
-
Save iburlakov/bf12ddbba474093899f4 to your computer and use it in GitHub Desktop.
Get domain name from dsconfigad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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