This commit is contained in:
crusader 2018-07-04 12:11:33 +09:00
parent 9a91d883d4
commit 8f73e25dd7

View File

@ -1,15 +1,8 @@
package ssh package ssh
import ( import (
"bufio"
"bytes"
"fmt"
uuid "github.com/satori/go.uuid"
ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig" ocmsc "git.loafle.net/overflow/commons-go/model/sensorconfig"
"git.loafle.net/overflow/container_network/crawler/ssh/client" "git.loafle.net/overflow/container_network/crawler/ssh/client"
"git.loafle.net/overflow/container_network/crawler/ssh/parser"
crawler "git.loafle.net/overflow/crawler-go" crawler "git.loafle.net/overflow/crawler-go"
) )
@ -30,59 +23,59 @@ func (c *SSHCrawler) Auth(auth map[string]interface{}) error {
} }
func (c *SSHCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, error) { func (c *SSHCrawler) Get(config *ocmsc.SensorConfig) (map[string]string, error) {
sshClient, err := client.New(config.Target) _, err := client.New(config.Connection)
if nil != err { if nil != err {
return nil, err return nil, err
} }
itemCount := len(config.Items) // itemCount := len(config.Items.MetaCollectionItems)
results := make(map[string]string, 0) results := make(map[string]string, 0)
boundary := uuid.NewV4().String() // boundary := uuid.NewV4().String()
commands := "" // commands := ""
for i := 0; i < itemCount; i++ { // for i := 0; i < itemCount; i++ {
switch i { // switch i {
case 0: // case 0:
commands = config.Items[i].QueryInfo.Query // commands = config.Items[i].QueryInfo.Query
default: // default:
commands = fmt.Sprintf("%s ; echo \"--%s\" ; %s ", commands, boundary, config.Items[i].QueryInfo.Query) // commands = fmt.Sprintf("%s ; echo \"--%s\" ; %s ", commands, boundary, config.Items[i].QueryInfo.Query)
} // }
} // }
commands = fmt.Sprintf("%s ; echo \"--%s--\" ", commands, boundary) // commands = fmt.Sprintf("%s ; echo \"--%s--\" ", commands, boundary)
buf, err := sshClient.RunCommand(commands) // buf, err := sshClient.RunCommand(commands)
if nil != err { // if nil != err {
return nil, err // return nil, err
} // }
r := bytes.NewReader(buf) // r := bytes.NewReader(buf)
scanner := bufio.NewScanner(r) // scanner := bufio.NewScanner(r)
pScanner := parser.NewParserScanner(scanner, boundary) // pScanner := parser.NewParserScanner(scanner, boundary)
for i := 0; i < itemCount; i++ { // for i := 0; i < itemCount; i++ {
item := config.Items[i] // item := config.Items[i]
mode := item.QueryInfo.Extend["mode"].(string) // mode := item.QueryInfo.Extend["mode"].(string)
p := parser.GetParser(mode) // p := parser.GetParser(mode)
if nil == p { // if nil == p {
return nil, fmt.Errorf("Container: Parser[%s] is not exist", mode) // return nil, fmt.Errorf("Container: Parser[%s] is not exist", mode)
} // }
rm, err := p.Parse(pScanner) // rm, err := p.Parse(pScanner)
if nil != err { // if nil != err {
return nil, err // return nil, err
} // }
if nil != rm { // if nil != rm {
mm := ocmsc.KeysToMap(item.Keys) // mm := ocmsc.KeysToMap(item.Keys)
for key, value := range mm { // for key, value := range mm {
results[value] = rm[key] // results[value] = rm[key]
} // }
} // }
if !pScanner.Clean() { // if !pScanner.Clean() {
break // break
} // }
} // }
return results, nil return results, nil
} }