简体   繁体   English

Mac OS X上的NetBIOS主机名解析

[英]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. 我能够使用IP扫描来获取IPv4,MAC等,我正在寻找NetBIOS,WINS主机名。 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. 我正在使用下面的代码,它对于公共IP正常工作,但是当我尝试使用工作位置的私有IP地址时,它给出了错误,可能是由于代理。 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. 经过数天的搜寻后,我终于发现没有可以直接获取它的api。 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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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