zoukankan      html  css  js  c++  java
  • IP 跟踪

    #coding=utf-8
    import sys
    import os
    import re
    import urllib
    import subprocess

    def getlocation(ip):
       result = urllib.urlopen("http://www.ip138.com/ips.asp?ip=%s&action=2" % ip)
      res = result.readlines()
      result.close()
      for i in res:
        if re.match(".*ul class="ul1".*",i):
          ipblock=i
      if 'ipblock' in dir():
        add1 = ipblock.split("<")[3].split(">")[1].decode('gb2312')[6:].encode('utf8')
        add2 = ipblock.split("<")[5].split(">")[1].decode('gb2312')[6:].encode('utf8')
        if add1 == add2:
          return " "+add1
        else:
          return " "+add1+" OR "+add2

    if len(sys.argv) < 2:
      print "Usage: %s {hostname|ip}" % sys.argv[0]
      sys.exit()
    else:
      host = sys.argv[1]

    try:
      p = subprocess.Popen(['tracert',host],stdout=subprocess.PIPE)
      while True:
        line = p.stdout.readline()
        if not line:
          break
        if re.match("^.[0-9].*(.*).*",line):
          try:
            ip = line.split('(')[1].split(')')[0]
            print line+getlocation(ip)
          except IndexError,e:
            print line,
        else:
          print line,
    except (KeyboardInterrupt,SystemExit):
      sys.exit()

    执行方法,到目录下 tracert.py www.baidu.com

  • 相关阅读:
    存储引擎的优缺点及增删改查基本操作
    安装Mariadb
    Mysql 入门概念
    Nginx语法着色
    find用法,文件压缩和lsof和cpio
    软件包管理
    Django 生成六位随机图片验证码
    Django自定义过滤器和自定义标签
    Django零碎知识点
    jQuery实现淡入淡出样式轮播
  • 原文地址:https://www.cnblogs.com/monsteryang/p/6569276.html
Copyright © 2011-2022 走看看