patn

package
v0.0.0-...-e949fbd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2016 License: MIT Imports: 9 Imported by: 0

README

Tutorial

package main

import (
	"fmt"
	"regexp"
	"strings"

	"github.com/abo/graw/patn"
)

func main() {
	mailsv := `Thu May 15 2015 00:15:05 mailsv1 sshd[2716]: Failed password for invalid user postgres from 86.212.199.60 port 4093 ssh2
Thu May 15 2015 00:15:05 mailsv1 sshd[2596]: Failed password for invalid user whois from 86.212.199.60 port 3311 ssh2
Thu May 15 2015 00:15:05 mailsv1 sshd[24947]: pam_unix(sshd:session): session opened for user djohnson by (uid=0)
Thu May 15 2015 00:15:05 mailsv1 sshd[3006]: Failed password for invalid user info from 86.212.199.60 port 4078 ssh2
Thu May 15 2015 00:15:05 mailsv1 sshd[5298]: Failed password for invalid user postgres from 86.212.199.60 port 1265 ssh2`
	lines := strings.Split(mailsv, "\n")

	patner := patn.NewPatner()
	exprs, err := patner.Generate("Thu May 15 2015 00:15:05 mailsv1 sshd[2716]: Failed password for invalid user postgres from 86.212.199.60 port 4093 ssh2",
			[]string{"2716"})

	if err != nil {
		panic(err)
	}

	extractor, err := patn.NewExtractor(exprs)
	if err != nil {
		panic(err)
	}

	sep := ""
	for i, l := range lines {
		fmt.Print(sep)
		fmt.Print(extractor.Extract(l))
		sep = " "
	}
	// Output: [2716] [2596] [24947] [3006] [5298]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Extractor

type Extractor struct {
	// contains filtered or unexported fields
}

Extractor a field extractor

func NewExtractor

func NewExtractor(exprs []string) (Extractor, error)

NewExtractor create a new extractor

func (*Extractor) Extract

func (e *Extractor) Extract(raw string) []string

Extract extract fields from raw

type Patner

type Patner struct {
	// contains filtered or unexported fields
}

Patner a pattern generator

func NewPatner

func NewPatner() Patner

NewPatner create a new pattern generator

func (*Patner) Generate

func (p *Patner) Generate(raw string, targets []string) ([]string, error)

Generate generate pattern to extract targets from raw

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL