简体   繁体   中英

NetBIOS host name resolution on Mac OS X

I am scanning devices information on local network using different protocols. I am able to get IPv4, MAC etc using IP Scanning, I am looking to get the NetBIOS, WINS host name. I am using the code below, it works fine for public ip's but it gives error when I try with private IP address of work location, may be due to proxy. Can any one help me ????

const char *ipstr = "172.17.241.xxx"; // or www.google.co.in
struct in_addr ip;
struct hostent *hp;

if (!inet_aton(ipstr, &ip))
    errx(1, "can't parse IP address %s", ipstr);

if ((hp = gethostbyaddr((const void *)&ip, sizeof ip, AF_INET)) == NULL){
    errx(1, "no name associated with %s", ipstr);
}

printf("h_error is %d\n", h_errno);
printf("name associated with %s is %s\n", ipstr, hp->h_name);

After days of googling finally I found that there is no api to get it directly. However one can fire the terminal command and read its output to get the specific details. Here is the code:

- (NSString *) runCommand: (NSString *) commandToRun
{
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/sh"];

    NSArray *arguments = [NSArray arrayWithObjects:
                          @"-c" ,
                          [NSString stringWithFormat:@"%@", commandToRun],
                          nil];
    NSLog(@"run command: %@",commandToRun);
    [task setArguments: arguments];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];

    [task launch];

    NSData *data;
    data = [file readDataToEndOfFile];

    NSString *output;
    output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

    return output;

}

then Call it

[self runCommand:@"smbutil status 172.168.234.111"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM