[英]indexing output of linux command in c++
我想获取给定URL的IP地址。
我目前正在使用
std::string i;
std::string pingStr = (std::string)"nslookup " +"www.yahoo.com" ;
i = system (pingStr.c_str());
但输出是
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
问 :有什么我只能获得IP地址吗?
使用getaddrinfo(3)函数以可用形式查找IP地址(IPv4或IPv6)。
您可以使用以下命令。
nslookup www.yahoo.com | grep Address: | sed -n 2p
grep地址为您提供了所有带有“地址”字样的行
sed获取那2的第二行
您可以在c ++中截断输出的“地址”部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.