Python generates logs via configuration file __python

Source: Internet
Author: User

Logtemplate.py:

#coding: Utf-8 import configparser from string import Template import time import math import Queue from getip import * de F getlogbyconf (num): CF = Configparser.configparser () cf.read ("logtemplate.conf") opts = Cf.options (cf.sectio NS () [0]) T = Template (Cf.get (Cf.sections () [0],opts[0]) dic={} for I in range (1,len (opts)): dic[opts[ I]] = Cf.get (Cf.sections () [0],opts[i]) dic[' date '] = Time.ctime () Getsysip = GetIP () dic[' sysip '] = GETSYSIP.G
    Et_ip_address () return T.substitute (DIC) * num def getlogbyscreenplay (num): CF = Configparser.configparser ()

    Cf.read ("logtemplate.conf") opts = Cf.options (cf.sections () [0]) T = Template (Cf.get (Cf.sections () [0],opts[0]) dic={} for I in range (1,len (opts)): dic[opts[i]] = Cf.get (Cf.sections () [0],opts[i]) dic[' date '] = time . CTime () Getscreenplayip = GetIP () dic[' sysip '] = getscreenplayip.get_ip_address () dic[' srcip '] = Getscreenpla
Yip.getipbyscreenplay ()
    Return T.substitute (DIC) * num def getlogbyrandom (num): CF = Configparser.configparser () cf.read ("Logtemplat  E.conf ") opts = Cf.options (cf.sections () [0]) T = Template (Cf.get (Cf.sections () [0],opts[0])) dic={} for I In range (1,len (opts)): dic[opts[i] = Cf.get (Cf.sections () [0],opts[i]) Getscreenplayip = GetIP () dic[' sys IP ' = getscreenplayip.get_ip_address () q = Queue.queue () for I in Range (0,num): dic[' Date ' = Time.ctime () Getrandomip = GetIP () dic[' srcip '] = getrandomip.getipbyrandom () dic[' srcport '] = Int (random.un Iform (0, 255)) log = T.substitute (DIC) q.put (log) return q;

Getip.py:

#coding: Utf-8 import configparser import OS import socket import fcntl import struct import sys import re import random ""
        "Gets the host IP address parameter ifname: The loopback address obtained via ' Lo '," "" "" Class Getip:def get_ip_address (self,ifname = ' lo ') obtained by ' eth0 ' for the host IP address: Try:s = Socket.socket (socket.af_inet, socket. SOCK_DGRAM) return Socket.inet_ntoa (Fcntl.ioctl (S.fileno), 0x8915, # Siocgif ADDR struct.pack (' 256s ', ifname[:15]) [20:24]) except:ips = Os.popen ("LA Ng=c Ifconfig | grep \ "Inet addr\" | GREP-V \ "127.0.0.1\" "" | Awk-f \ ": \" ' {print $} ' | awk ' {print '} ' "). ReadLines () If Len (IPs) > 0:return ips[0] return ' def ge Tipbyconf (self): CF = Configparser.configparser () cf.read ("logtemplate.conf") opts = cf.options (cf . sections () [0]) if ' Srcip ' in Opts:return cf.get (cf.sections () [0],' Srcip ') Else:return ' profile Not Configured IP field ' def getipbyscreenplay (self): Ipbyscreenplay = Raw_inp UT ("Please enter ip:\n") IsIP = Re.search (' ^ [01]?\d\d?| 2[0-4]\D|25[0-5]) \. ([01]?\d\d?|
                  2[0-4]\D|25[0-5]) \. ' ' ([01]?\d\d?| 2[0-4]\D|25[0-5]) \. ([01]?\d\d?| 2[0-4]\D|25[0-5]) $ ', ipbyscreenplay) if Isip:return ipbyscreenplay Else:return ' 127.0.0.1 ' Def getipbyrandom (self): Ipbyrandom = (str (int (random.uniform (0, 255))) + '. ' + str (int (random.uni
        form (0, 255))) + '. ' + str (int (random.uniform (0, 255)) + '. ' + str (int (random.uniform (0, 255))) Return Ipbyrandom

Logtemplate.conf:

[Conf]
Template = $date $sysip sshd[$shdcode]: $responsetype for $user from $srcip Port $srcport ssh2
sysip = 127.0.0.1
s Hdcode = 6666
responsetype = Failed password
user = root
srcip = 255.255.255.0 srcport
= 6666
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.