-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4b375b5
Showing
11 changed files
with
513 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 neko-neko | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# utmpdump | ||
utmpdump is output utmp format file to json or tsv or csv. | ||
|
||
## Installation | ||
**[Download latest binary](https://github.com/neko-neko/utmpdump/releases/latest)** | ||
|
||
Multi-platform support | ||
- Windows | ||
- Mac OS X | ||
- Linux 32,64bit | ||
|
||
Or get the library | ||
``` | ||
$ go get github.com/neko-neko/utmpdump | ||
``` | ||
Or clone the repository and run | ||
``` | ||
$ go install github.com/neko-neko/utmpdump | ||
``` | ||
|
||
## Usage | ||
``` | ||
Usage of utmpdump: utmpdump [options] | ||
-f, --file <file> load a specific file instead of utmp | ||
-t, --until <YYYYMMDDHHMMSS> display the lines until the specified time | ||
-s, --since <YYYYMMDDHHMMSS> display the lines since the specified time | ||
-o, --output <json/tsv/csv> display the lines at specific format. Default format is json. | ||
``` | ||
|
||
### Example | ||
``` | ||
$ utmpdump -f /var/log/wtmp | ||
``` | ||
|
||
## Contributing | ||
1. Fork it! | ||
2. Create your feature branch: `git checkout -b my-new-feature` | ||
3. Commit your changes: `git commit -am 'Add some feature'` | ||
4. Push to the branch: `git push origin my-new-feature` | ||
5. Submit a pull request :D | ||
|
||
## Credits | ||
neko-neko | ||
|
||
## License | ||
MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package filter | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/neko-neko/utmpdump/utmp" | ||
) | ||
|
||
type TimeFilter struct{} | ||
|
||
// filter time | ||
func (f *TimeFilter) Filter(utmps []*utmp.GoUtmp, since time.Time, until time.Time) []*utmp.GoUtmp { | ||
var ( | ||
result = []*utmp.GoUtmp{} | ||
sinceIsZero = since.IsZero() | ||
untilIsZero = until.IsZero() | ||
) | ||
|
||
if sinceIsZero && untilIsZero { | ||
return utmps | ||
} | ||
|
||
if !sinceIsZero { | ||
for _, u := range utmps { | ||
utime, _ := time.Parse(utmp.TimeFormat, u.Time) | ||
if !utime.Before(since) { | ||
result = append(result, u) | ||
} | ||
} | ||
} | ||
|
||
if !untilIsZero { | ||
for _, u := range utmps { | ||
utime, _ := time.Parse(utmp.TimeFormat, u.Time) | ||
if !utime.After(until) { | ||
result = append(result, u) | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/neko-neko/utmpdump/filter" | ||
"github.com/neko-neko/utmpdump/printer" | ||
"github.com/neko-neko/utmpdump/utmp" | ||
) | ||
|
||
func main() { | ||
var ( | ||
filePath string | ||
until string | ||
since string | ||
outputType string | ||
) | ||
|
||
flag.Usage = func() { | ||
fmt.Fprintf(os.Stderr, `Usage of %s: %s [options] | ||
-f, --file <file> load a specific file instead of utmp | ||
-t, --until <YYYYMMDDHHMMSS> display the lines until the specified time | ||
-s, --since <YYYYMMDDHHMMSS> display the lines since the specified time | ||
-o, --output <json/tsv/csv> display the lines at specific format. Default format is json. | ||
`, os.Args[0], os.Args[0]) | ||
} | ||
|
||
// parse arguments | ||
flag.StringVar(&filePath, "f", "", "file path") | ||
flag.StringVar(&filePath, "file", "", "file path") | ||
flag.StringVar(&until, "t", "", "until") | ||
flag.StringVar(&until, "until", "", "until") | ||
flag.StringVar(&since, "s", "", "since") | ||
flag.StringVar(&since, "since", "", "since") | ||
flag.StringVar(&outputType, "o", "", "output type") | ||
flag.StringVar(&outputType, "output", "", "output type") | ||
flag.Parse() | ||
if filePath == "" { | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
outputType = strings.ToLower(outputType) | ||
if _, exist := printer.PrintTypes[outputType]; outputType != "" && !exist { | ||
flag.Usage() | ||
os.Exit(1) | ||
} | ||
sinceTime, _ := time.Parse("20060102150405", since) | ||
untilTime, _ := time.Parse("20060102150405", until) | ||
|
||
// read file | ||
file, openErr := os.Open(filePath) | ||
defer file.Close() | ||
if openErr != nil { | ||
fmt.Fprintln(os.Stderr, openErr) | ||
os.Exit(1) | ||
} | ||
utmps, readErr := utmp.Read(file) | ||
if readErr != nil { | ||
fmt.Fprintln(os.Stderr, readErr) | ||
os.Exit(1) | ||
} | ||
|
||
// to go utmp | ||
var goUtmps []*utmp.GoUtmp | ||
for _, gu := range utmps { | ||
goUtmps = append(goUtmps, utmp.ToGoUtmp(gu)) | ||
} | ||
|
||
// filter | ||
filter := &filter.TimeFilter{} | ||
goUtmps = filter.Filter(goUtmps, sinceTime, untilTime) | ||
|
||
printer := printer.GetPrinter(outputType) | ||
for _, u := range goUtmps { | ||
printer.Print(u) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package printer | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/neko-neko/utmpdump/utmp" | ||
) | ||
|
||
// TSV printer | ||
type CsvPrinter struct{} | ||
|
||
// Format TSV | ||
func (t *CsvPrinter) Print(u *utmp.GoUtmp) { | ||
fmt.Printf( | ||
"%d,%d,%s,%s,%s,%s,%d,%d,%d,%s,%s\n", | ||
u.Type, | ||
u.Pid, | ||
u.Device, | ||
u.Id, | ||
u.User, | ||
u.Host, | ||
u.Exit.Exit, | ||
u.Exit.Termination, | ||
u.Session, | ||
u.Time, | ||
u.Addr, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package printer | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/neko-neko/utmpdump/utmp" | ||
) | ||
|
||
// TSV printer | ||
type JsonPrinter struct{} | ||
|
||
// Format TSV | ||
func (t *JsonPrinter) Print(u *utmp.GoUtmp) { | ||
json, _ := json.Marshal(u) | ||
fmt.Println(string(json)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package printer | ||
|
||
import ( | ||
"github.com/neko-neko/utmpdump/utmp" | ||
) | ||
|
||
const ( | ||
PrintTypeJson = "json" | ||
PrintTypeCsv = "csv" | ||
PrintTypeTsv = "tsv" | ||
) | ||
|
||
var PrintTypes = map[string]struct{}{ | ||
PrintTypeJson: struct{}{}, | ||
PrintTypeCsv: struct{}{}, | ||
PrintTypeTsv: struct{}{}, | ||
} | ||
|
||
// Printer interface | ||
type Printer interface { | ||
Print(utmp *utmp.GoUtmp) | ||
} | ||
|
||
// Get formatter | ||
func GetPrinter(formatType string) Printer { | ||
switch formatType { | ||
case PrintTypeJson: | ||
return &JsonPrinter{} | ||
case PrintTypeCsv: | ||
return &CsvPrinter{} | ||
case PrintTypeTsv: | ||
return &TsvPrinter{} | ||
default: | ||
return &JsonPrinter{} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package printer | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/neko-neko/utmpdump/utmp" | ||
) | ||
|
||
// TSV printer | ||
type TsvPrinter struct{} | ||
|
||
// Format TSV | ||
func (t *TsvPrinter) Print(u *utmp.GoUtmp) { | ||
fmt.Printf( | ||
"%d\t%d\t%s\t%s\t%s\t%s\t%d\t%d\t%d\t%s\t%s\n", | ||
u.Type, | ||
u.Pid, | ||
u.Device, | ||
u.Id, | ||
u.User, | ||
u.Host, | ||
u.Exit.Exit, | ||
u.Exit.Termination, | ||
u.Session, | ||
u.Time, | ||
u.Addr, | ||
) | ||
} |
Oops, something went wrong.