python路由跟踪(闲来无聊写着玩)

2023-01-31 02:01:10 路由 写着 闲来

源代码如下:

#coding=gbk
import urllib,re
import os,sys
from bs4 import BeautifulSoup as soup
def get_ip(ip):
    a = []
    url = "Http://www.ip138.com/ips138.asp?ip={0}&action=2".fORMat(ip)
    opurl = urllib.urlopen(url)
    o_data = opurl.read()
    opurl.close()
    c = soup(o_data)
    data = c.find_all("table",{"width":"80%"})
    for x in  data:
        x_l = ''.join(re.findall(u'本站主数据:(.*)',x.li.text))
        return  '    %s  %s \n' % (ip,x_l)
       
def get_tracert(domain):
    ip_list = []
    data = os.popen('tracert -d %s ' % domain).readlines()
    b = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
    data = [  b.findall(x) for x in data ]
    for x in data:
        if x != []:
            ip_list.append(''.join(x))
    return ip_list
if __name__ == '__main__':
    print '访问%s 经过的路由如下:\n' % sys.argv[1]
    for x in get_tracert(sys.argv[1]):
        print get_ip(x)


保存成tracert.py


执行结果如下:

224459696.jpg



相关文章