Python script gets the server external connection IP and removes the IP that is associated with the non-local server program

Source: Internet
Author: User

#!/bin/env python#-*- coding:utf-8 -*-import osimport sysfrom subprocess  IMPORT POPENDEF 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)  &Nbsp;      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__ ':    &nbsP;    get_foriegn_ip ()         list_different ()         get_time ()


The IP that is associated with this server program is written to the file first Localgroup.txt

Python script gets the server external connection IP and removes the IP that is associated with the non-local server program

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.