Mac OS X上的NetBIOS主机名解析

时间:2015-06-02 13:55:15

标签: objective-c macos swift networking

我正在使用不同协议扫描本地网络上的设备信息。我能够使用IP扫描获得IPv4,MAC等,我希望得到NetBIOS,WINS主机名。我正在使用下面的代码,它适用于公共IP,但它在我尝试使用工作位置的私有IP地址时出错,可能是由于代理。任何人都可以帮助我????

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);

1 个答案:

答案 0 :(得分:0)

经过几天的谷歌搜索后,我发现没有api可以直接获取它。但是,可以触发终端命令并读取其输出以获取特定详细信息。这是代码:

- (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;

}

然后调用它

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