From 6a62fc3d391292430482dd56f2834f7e4c453b41 Mon Sep 17 00:00:00 2001 From: XinRoom <32238570+XinRoom@users.noreply.github.com> Date: Fri, 17 Mar 2023 09:22:02 +0800 Subject: [PATCH] =?UTF-8?q?mod:=20=E9=BB=98=E8=AE=A4=E4=B8=8D=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=88=B0=E6=96=87=E4=BB=B6=EF=BC=8C=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E6=8C=87=E5=AE=9A`-o=20filename`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/go-portScan.go | 10 +++++++++- util/log.go | 12 ++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cmd/go-portScan.go b/cmd/go-portScan.go index 73625f3..e79d727 100644 --- a/cmd/go-portScan.go +++ b/cmd/go-portScan.go @@ -37,6 +37,7 @@ var ( netLive bool maxOpenPort int oCsv string + oFile string ) func parseFlag(c *cli.Context) { @@ -56,14 +57,15 @@ func parseFlag(c *cli.Context) { netLive = c.Bool("netLive") maxOpenPort = c.Int("maxOpenPort") oCsv = c.String("oCsv") + oFile = c.String("oFile") } func run(c *cli.Context) error { - myLog := util.NewLogger("output.log", true) if c.NumFlags() == 0 { cli.ShowAppHelpAndExit(c, 0) } parseFlag(c) + myLog := util.NewLogger(oFile, true) if devices { if r, err := syn.GetAllDevs(); err != nil { myLog.Fatal(err.Error()) @@ -430,6 +432,12 @@ func main() { Usage: "output csv file", Value: "", }, + &cli.StringFlag{ + Name: "oFile", + Aliases: []string{"o"}, + Usage: "output to file", + Value: "", + }, }, } diff --git a/util/log.go b/util/log.go index 935a10c..c236bd8 100644 --- a/util/log.go +++ b/util/log.go @@ -8,11 +8,15 @@ import ( func NewLogger(filename string, std bool) *log.Logger { var out io.Writer - outFile, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) - if std { - out = io.MultiWriter(os.Stdout, outFile) + if filename == "" { + out = os.Stdout } else { - out = outFile + outFile, _ := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) + if std { + out = io.MultiWriter(os.Stdout, outFile) + } else { + out = outFile + } } logger := log.New(out, "", 0) return logger