简体   繁体   English

在C ++中索引Linux命令的输出

[英]indexing output of linux command in c++

I want to get the ip address given url. 我想获取给定URL的IP地址。

I am currently using this 我目前正在使用

        std::string i;
        std::string pingStr = (std::string)"nslookup " +"www.yahoo.com" ;
        i = system (pingStr.c_str());

but the output is 但输出是

Server:     127.0.1.1
Address:    127.0.1.1#53

Non-authoritative answer:
www.yahoo.com   canonical name = atsv2-fp-shed.wg1.b.yahoo.com.
Name:   atsv2-fp-shed.wg1.b.yahoo.com
Address: 106.10.250.10

Q : Is there anyway I can only get the Ip address ? :有什么我只能获得IP地址吗?

使用getaddrinfo(3)函数以可用形式查找IP地址(IPv4或IPv6)。

you can use the folowing command. 您可以使用以下命令。

nslookup www.yahoo.com | grep Address: |  sed -n 2p

grep Address gives you all lines having "address" word in them grep地址为您提供了所有带有“地址”字样的行

sed gets the 2nd line of those 2 sed获取那2的第二行

You can truncate the "Address" part of output in c++. 您可以在c ++中截断输出的“地址”部分。

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

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