!83 添加了LogInit函数打印日志

Merge pull request !83 from 宋帮诚晋/master
This commit is contained in:
宋帮诚晋 2022-11-16 07:59:45 +00:00 committed by Gitee
commit 99fff42d6e
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 91 additions and 84 deletions

2
logfile Normal file
View File

@ -0,0 +1,2 @@
2022/11/16 15:54:27 命令输出: uid=1000(song) gid=1000(song) 组=1000(song),4(adm),24(cdrom),27(sudo),29(audio),30(dip),46(plugdev),118(lpadmin),129(sambashare)

BIN
main

Binary file not shown.

View File

@ -7,11 +7,10 @@ import (
"flag"
"strconv"
"strings"
genmai "main/src/genmai"
)
type Vul struct{
ParserNum int //协程数
ParserNum int //协程数
System string //执行系统漏洞检测
Web string //Web漏洞检测
Kernel string //内核漏洞检测
@ -28,7 +27,7 @@ type Vul struct{
SSHPassword string //密码
}
func main(){
genmai.LogInit()
//检测架构是否符合漏扫架构
// result:=Actuator.OsCheck()
// if result=="flase"{

View File

@ -9,7 +9,7 @@ import(
func ArgCheck(targetString string)(LegalValue string){
match, err := regexp.MatchString(`^[a-zA-Z][a-zA-Z0-9-]{4,15}$`, targetString)
if err != nil {
fmt.Println("IP 不合法请修改")
fmt.Println("参数不合规")
}
// fmt.Println(match)
LegalValue=strconv.FormatBool(match)

View File

@ -25,7 +25,7 @@ func NewPool(cap ...int) *Pool {
// p 是 Pool的引用
p := &Pool{
TaskChannel: make(chan func()),
GoNum: n,
GoNum: n,
}
return p
}
@ -48,7 +48,7 @@ func (p *Pool) Submit(f func()) {
}
func CoprogramPool(Num int,RequestsSystem string,RequestsKernel string,RequestsWeb string,RequestsBaseLine string,PoolStatNums int,IP string) {
p := NewPool(Num)
p := NewPool(0)
StartPool(p)
var wg sync.WaitGroup
wg.Add(PoolStatNums)

17
src/genmai/log.go Normal file
View File

@ -0,0 +1,17 @@
package genmai
import (
"log"
"os"
)
func LogInit() {
// 获取日志文件句柄
// 以 只写入文件|没有时创建|文件尾部追加 的形式打开这个文件
logFile, err := os.OpenFile(`./logfile`, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err)
}
// 设置存储位置
log.SetOutput(logFile)
}

BIN
test

Binary file not shown.

145
test.go
View File

@ -1,87 +1,76 @@
package main
import (
"fmt"
"golang.org/x/crypto/ssh"
"log"
"time"
"fmt"
"math/rand"
"sync"
"time"
)
func main(){
type Task struct {
id int
randnum int
}
type Result struct {
task Task
result int
}
sshHost := "127.0.0.1"
var tasks = make(chan Task, 10)
var results = make(chan Result, 10)
sshUser := "song"
sshPassword := "sbcj1999"
sshType := "password"
sshPort := 22
//创建sshp登陆配置
config := &ssh.ClientConfig{
Timeout: 5*time.Second,//ssh 连接time out 时间一秒钟, 如果ssh验证错误 会在一秒内返回
User: sshUser,
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //这个可以, 但是不够安全
//HostKeyCallback: hostKeyCallBackFunc(h.Host),
}
if sshType == "password" {
config.Auth = []ssh.AuthMethod{ssh.Password(sshPassword)}
}
//dial 获取ssh client
addr := fmt.Sprintf("%s:%d", sshHost, sshPort)
sshClient, err := ssh.Dial("tcp", addr, config)
if err != nil {
log.Fatal("创建ssh client 失败",err)
}
defer sshClient.Close()
//创建ssh-session
session, err := sshClient.NewSession()
if err != nil {
log.Fatal("创建ssh session 失败",err)
}
defer session.Close()
//执行远程命令
command:="whoami"
combo,err := session.CombinedOutput(command)
if err != nil {
log.Fatal("远程执行cmd 失败",err)
}else{
log.Println("命令输出:",string(combo))
defer session.Close()
func process(num int) int {
sum := 0
for num != 0 {
digit := num % 10
sum += digit
num /= 10
}
time.Sleep(2 * time.Second)
return sum
}
func worker(wg *sync.WaitGroup) {
defer wg.Done()
for task := range tasks {
result := Result{task, process(task.randnum)}
results <- result
}
}
func createWorkerPool(numOfWorkers int) {
var wg sync.WaitGroup
for i := 0; i < numOfWorkers; i++ {
wg.Add(1)
go worker(&wg)
}
wg.Wait()
close(results)
}
func allocate(numOfTasks int) {
for i := 0; i < numOfTasks; i++ {
randnum := rand.Intn(999)
task := Task{i, randnum}
tasks <- task
}
close(tasks)
}
func getResult(done chan bool) {
for result := range results {
fmt.Printf("Task id %d, randnum %d , sum %d\n", result.task.id, result.task.randnum, result.result)
}
done <- true
}
func main() {
startTime := time.Now()
numOfWorkers := 200
numOfTasks := 100
}
var done = make(chan bool)
go getResult(done)
go allocate(numOfTasks)
go createWorkerPool(numOfWorkers)
// 必须在allocate()和getResult()之后创建工作池
<-done
endTime := time.Now()
diff := endTime.Sub(startTime)
fmt.Println("total time taken ", diff.Seconds(), "seconds")
}