Created
March 13, 2013 07:14
-
-
Save keefo/5149929 to your computer and use it in GitHub Desktop.
get router MACAddress
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 *)routerMACAddress { | |
NSMutableString *MACAddress = [NSMutableString string]; | |
SCDynamicStoreRef theDynamicStore = SCDynamicStoreCreate(nil, CFSTR("FindCurrentInterfaceAndIP"), nil, nil); | |
CFDictionaryRef returnedPList = SCDynamicStoreCopyValue(theDynamicStore, CFSTR("State:/Network/Global/IPv4")); | |
CFRelease(theDynamicStore); | |
if ([(__bridge NSDictionary *)returnedPList valueForKey:@"Router"] != nil) { | |
NSTask *arpTask = [[NSTask alloc] init]; | |
NSPipe *newPipe = [NSPipe pipe]; | |
NSFileHandle *readHandle = [newPipe fileHandleForReading]; | |
[arpTask setStandardOutput:newPipe]; | |
[arpTask setLaunchPath:@"/usr/sbin/arp"]; | |
[arpTask setArguments:[NSArray arrayWithObject:[(__bridge NSDictionary *)returnedPList valueForKey:@"Router"]]]; | |
[arpTask launch]; | |
[arpTask waitUntilExit]; | |
NSString *outputString = [[NSString alloc] initWithData:[readHandle availableData] encoding:NSUTF8StringEncoding]; | |
int i=0; | |
int spaceCount = 0; | |
for (i=0; i<[outputString length]; i++) { | |
if ([outputString characterAtIndex:i] == ' ') { | |
spaceCount++; | |
}; | |
if (spaceCount == 3 && [outputString characterAtIndex:i] != ' ') { | |
[MACAddress appendFormat:@"%c",[outputString characterAtIndex:i]]; | |
}; | |
}; | |
}; | |
return MACAddress; | |
} | |
- (NSString *)defaultRouter { | |
SCDynamicStoreRef ds = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("myapp"), NULL, NULL); | |
CFDictionaryRef dr = SCDynamicStoreCopyValue(ds, CFSTR("State:/Network/Global/IPv4")); | |
CFStringRef router = CFDictionaryGetValue(dr, CFSTR("Router")); | |
NSString *routerString = [NSString stringWithString:(__bridge NSString *)router]; | |
CFRelease(dr); | |
CFRelease(ds); | |
return routerString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment