mirror of
https://github.com/bettercap/bettercap
synced 2025-08-14 10:46:57 -07:00
new: new ticker module with netmon.cap example caplet
This commit is contained in:
parent
b632b6d2d7
commit
73c13beb8a
3 changed files with 114 additions and 0 deletions
109
modules/ticker.go
Normal file
109
modules/ticker.go
Normal file
|
@ -0,0 +1,109 @@
|
|||
package modules
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/evilsocket/bettercap-ng/log"
|
||||
"github.com/evilsocket/bettercap-ng/session"
|
||||
)
|
||||
|
||||
type Ticker struct {
|
||||
session.SessionModule
|
||||
Period time.Duration
|
||||
Commands []string
|
||||
}
|
||||
|
||||
func NewTicker(s *session.Session) *Ticker {
|
||||
t := &Ticker{
|
||||
SessionModule: session.NewSessionModule("ticker", s),
|
||||
}
|
||||
|
||||
t.AddParam(session.NewStringParameter("ticker.commands",
|
||||
"clear; net.show",
|
||||
"",
|
||||
"List of commands separated by a ;"))
|
||||
|
||||
t.AddParam(session.NewIntParameter("ticker.period",
|
||||
"1",
|
||||
"Ticker period in seconds"))
|
||||
|
||||
t.AddHandler(session.NewModuleHandler("ticker on", "",
|
||||
"Start the ticker.",
|
||||
func(args []string) error {
|
||||
return t.Start()
|
||||
}))
|
||||
|
||||
t.AddHandler(session.NewModuleHandler("ticker off", "",
|
||||
"Stop the ticker.",
|
||||
func(args []string) error {
|
||||
return t.Stop()
|
||||
}))
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *Ticker) Name() string {
|
||||
return "ticker"
|
||||
}
|
||||
|
||||
func (t *Ticker) Description() string {
|
||||
return "A module to execute one or more commands every given amount of seconds."
|
||||
}
|
||||
|
||||
func (t *Ticker) Author() string {
|
||||
return "Simone Margaritelli <evilsocket@protonmail.com>"
|
||||
}
|
||||
|
||||
func (t *Ticker) Configure() error {
|
||||
var err error
|
||||
var commands string
|
||||
var period int
|
||||
|
||||
if err, commands = t.StringParam("ticker.commands"); err != nil {
|
||||
return err
|
||||
}
|
||||
t.Commands = session.ParseCommands(commands)
|
||||
|
||||
if err, period = t.IntParam("ticker.period"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.Period = time.Duration(period) * time.Second
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Ticker) Start() error {
|
||||
if t.Running() == true {
|
||||
return session.ErrAlreadyStarted
|
||||
} else if err := t.Configure(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.SetRunning(true)
|
||||
go func() {
|
||||
log.Info("Ticker running with period %ds.", t.Period)
|
||||
tick := time.Tick(t.Period)
|
||||
for _ = range tick {
|
||||
if t.Running() == false {
|
||||
break
|
||||
}
|
||||
|
||||
for _, cmd := range t.Commands {
|
||||
if err := t.Session.Run(cmd); err != nil {
|
||||
log.Error("%s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Ticker) Stop() error {
|
||||
if t.Running() == false {
|
||||
return session.ErrAlreadyStopped
|
||||
}
|
||||
t.SetRunning(false)
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue