简体   繁体   中英

indexing output of linux command in c++

I want to get the ip address given url.

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 ?

使用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

sed gets the 2nd line of those 2

You can truncate the "Address" part of output in c++.

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