#!/bin/env python
#-*- coding:utf-8 -*-
import os
import sys
from subprocess import Popen
def get_foriegn_ip():
l = os.popen("netstat -ant|grep ESTABLISHED|awk ‘{print $5}‘|awk -F: ‘{print $1}‘|sort -r|uniq -c |awk ‘{print $2}‘|grep -v ‘and‘|grep -v ‘Address‘|grep -v ‘0.0.0.0‘ > foreign_ip.txt")
def list_different():
list1=[]
f = open(‘foreign_ip.txt‘,‘r‘)
for line in f.readlines():
line1=line.replace("\n","")
list1.append(line1)
print(list1)
list2=[]
f = open(‘localgroup.txt‘,‘r‘)
for line in f.readlines():
line1=line.replace("\n","")
list2.append(line1)
print(list2)
l = [x for x in list1 if x not in list2]
print(l)
def get_time():
d = os.popen(‘echo "$(date +%Y%m%d_%H:%M:%S)">>/root/1.txt‘)
if __name__ == ‘__main__‘:
get_foriegn_ip()
list_different()
get_time()
首先将与本服务器程序有关联的IP写入文件localgroup.txt
原文:http://linuxpython.blog.51cto.com/10015972/1941045