1
0
mirror of https://github.com/zu1k/nali.git synced 2025-01-23 13:49:02 +08:00
nali/cmd/root.go

84 lines
1.4 KiB
Go
Raw Normal View History

2020-07-17 09:05:25 +08:00
package cmd
import (
2020-07-17 10:52:36 +08:00
"bufio"
2020-07-17 09:05:25 +08:00
"fmt"
2020-07-17 09:41:09 +08:00
"log"
"os"
2020-07-17 09:05:25 +08:00
"github.com/zu1k/nali/internal/app"
2020-07-17 13:49:11 +08:00
"github.com/zu1k/nali/internal/ipdb"
2020-07-17 09:05:25 +08:00
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "nali",
2020-07-17 13:49:11 +08:00
Short: "An offline tool for querying IP geographic information",
2020-07-18 10:53:44 +08:00
Long: `An offline tool for querying IP geographic information.
Find document on: https://github.com/zu1k/nali
#1 Query a simple IP address
$ nali 1.2.3.4
or use pipe
$ echo IP 6.6.6.6 | nali
#2 Query multiple IP addresses
$ nali 1.2.3.4 4.3.2.1 123.23.3.0
#3 Interactive query
$ nali
123.23.23.23
123.23.23.23 [越南 越南邮电集团公司]
quit
#4 Use with dig
$ dig nali.lgf.im +short | nali
#5 Use with nslookup
$ nslookup nali.lgf.im 8.8.8.8 | nali
#6 Use with any other program
bash abc.sh | nali
#7 IPV6 support
$ nslookup google.com | nali
`,
Args: cobra.MinimumNArgs(0),
2020-07-17 09:05:25 +08:00
Run: func(cmd *cobra.Command, args []string) {
2020-07-17 12:12:04 +08:00
app.InitIPDB(ipdb.GetIPDBType())
2020-07-19 07:36:58 +08:00
app.InitCDNDB()
2020-07-17 09:05:25 +08:00
if len(args) == 0 {
2020-07-17 10:52:36 +08:00
stdin := bufio.NewScanner(os.Stdin)
for stdin.Scan() {
line := stdin.Text()
if line == "quit" || line == "exit" {
return
}
2020-07-19 07:24:25 +08:00
fmt.Println(app.ReplaceIPInString(app.ReplaceCDNInString(line)))
2020-07-17 10:52:36 +08:00
}
} else {
app.ParseIPs(args)
2020-07-17 09:05:25 +08:00
}
},
}
2020-07-17 09:55:24 +08:00
// Execute parse subcommand and run
2020-07-17 09:05:25 +08:00
func Execute() {
if err := rootCmd.Execute(); err != nil {
2020-07-17 09:41:09 +08:00
log.Fatal(err.Error())
os.Exit(1)
2020-07-17 09:05:25 +08:00
}
}