需求
1,提取域名解析后的IP地址。
测试环境
Cent OS 7.x
Python 2.7+
Python 代码
import os import re def GetIp(domain): cmd = "ping -n -c 1 -w 1 {domain}".format(domain=domain) res = os.popen(cmd).read() ip = re.findall(r'(?:25[0-5]\.|2[0-4]\d\.|[01]?\d\d?\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)', res) if len(ip) == 0: return False return ip[0] if __name__ == '__main__': print(GetIp("sharebar.org"))
运行结果
[root@sharebar-org tmp]# python GetIp.py 119.167.164.17
转载请注明:分享吧 » Python 获取域名解析IP