日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

ip地址异常判定python_python检测异常ip,并查询ip详细信息

發布時間:2023/12/10 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ip地址异常判定python_python检测异常ip,并查询ip详细信息 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#!/usr/local/bin/python3

# coding:utf-8

import string, subprocess, send_mail

from time import strftime, gmtime, sleep, re

import urllib.request, re

def filter_tags(htmlstr):

re_cdata = re.compile('//]*//\]\]>', re.I) # 匹配CDATA

re_script = re.compile(']*>[^', re.I) # Script

re_style = re.compile(']*>[^', re.I) # style

re_br = re.compile('
') # 處理換行

re_h = re.compile('?\w+[^>]*>') # HTML標簽

re_comment = re.compile('') # HTML注釋

s = re_cdata.sub('', htmlstr) # 去掉CDATA

s = re_script.sub('', s) # 去掉SCRIPT

s = re_style.sub('', s) # 去掉style

s = re_br.sub('\n', s) # 將br轉換為換行

s = re_h.sub('', s) # 去掉HTML 標簽

s = re_comment.sub('', s)

blank_line = re.compile('\n+')

s = blank_line.sub('\n', s)

return s

def ip_info(ip):

# url = 'http://ip.taobao.com/service/getIpInfo.php?ip=%s' % ip

url = 'http://www.ip.cn/index.php?ip=%s' % ip

f = urllib.request.Request(url)

f.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0')

response = ((urllib.request.urlopen(f)).read()).decode('utf-8')

ip_information = (filter_tags(response)).split('\n\t')[19]

return ip_information

tcptmpStr = (

((subprocess.Popen("ss -antp |grep -v LISTEN", shell=True, stdout=subprocess.PIPE)).stdout.read()).decode()).strip()

whiteIp = ['127.0.0.1', 'x.x.x.x', 'x.x.x.x']

whitePort = ['80', '22', '3306']

# udptmpStr = ((subprocess.check_output(["netstat", "-nulp"])).decode('utf-8')).strip()

# get tcp connect

#

# def getTCPservice(tcptmpStr):

tmpList = tcptmpStr.split("\n")

del tmpList[0]

# newList = []

for i in tmpList:

val = i.split()

del val[0:3]

valTmpip = (val[1].split(":"))[-2] # remote addr

valTmpprot = (val[0].split(":"))[-1] # local port

if valTmpip not in whiteIp and valTmpprot not in whitePort:

with open('/var/openresty/nginx/logs/suspicious.txt', 'a') as f:

current_time = '#================<< Capture Time : ' + strftime("%Y-%m-%d %H:%M:%S",

gmtime()) + ' >>==============\n'

f.write(current_time)

f.write(val[2] + '\n')

f.write(valTmpip + '\n')

sleep(5)

ipInfo = ip_info(valTmpip)

f.write(ipInfo)

# send_mail.sendMail('/var/openresty/nginx/logs/suspicious.txt')

總結

以上是生活随笔為你收集整理的ip地址异常判定python_python检测异常ip,并查询ip详细信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。