代码拉取完成,页面将自动刷新
同步操作将从 mylxsw/remote-tail 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package main
import (
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"github.com/BurntSushi/toml"
"github.com/mylxsw/remote-tail/command"
"github.com/mylxsw/remote-tail/console"
)
var mossSep = ".--. --- .-- . .-. . -.. -... -.-- -- -.-- .-.. -..- ... .-- \n"
var welcomeMessage string = getWelcomeMessage() + console.ColorfulText(console.TextMagenta, mossSep)
var filePath *string = flag.String("file", "", "-file=\"/home/data/logs/**/*.log\"")
var hostStr *string = flag.String("hosts", "", "-hosts=root@192.168.1.225,root@192.168.1.226")
var configFile *string = flag.String("conf", "", "-conf=example.toml")
func usageAndExit(message string) {
if message != "" {
fmt.Fprintln(os.Stderr, message)
}
flag.Usage()
fmt.Fprint(os.Stderr, "\n")
os.Exit(1)
}
func printWelcomeMessage(config command.Config) {
fmt.Println(welcomeMessage)
for _, server := range config.Servers {
// If there is no tail_file for a service configuration, the global configuration is used
if server.TailFile == "" {
server.TailFile = config.TailFile
}
serverInfo := fmt.Sprintf("%s@%s:%s", server.User, server.Hostname, server.TailFile)
fmt.Println(console.ColorfulText(console.TextMagenta, serverInfo))
}
fmt.Printf("\n%s\n", console.ColorfulText(console.TextCyan, mossSep))
}
func parseConfig(filePath string, hostStr string, configFile string) (config command.Config) {
if configFile != "" {
if _, err := toml.DecodeFile(configFile, &config); err != nil {
log.Fatal(err)
}
} else {
var hosts []string = strings.Split(hostStr, ",")
config = command.Config{}
config.TailFile = filePath
config.Servers = make(map[string]command.Server, len(hosts))
for index, hostname := range hosts {
hostInfo := strings.Split(strings.Replace(hostname, ":", "@", -1), "@")
var port int
if len(hostInfo) > 2 {
port, _ = strconv.Atoi(hostInfo[2])
}
config.Servers["server_"+string(index)] = command.Server{
ServerName: "server_" + string(index),
Hostname: hostInfo[1],
User: hostInfo[0],
Port: port,
}
}
}
return
}
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, welcomeMessage)
fmt.Fprint(os.Stderr, "Options:\n\n")
flag.PrintDefaults()
}
flag.Parse()
if (*filePath == "" || *hostStr == "") && *configFile == "" {
usageAndExit("")
}
config := parseConfig(*filePath, *hostStr, *configFile)
printWelcomeMessage(config)
outputs := make(chan command.Message, 255)
var wg sync.WaitGroup
for _, server := range config.Servers {
wg.Add(1)
go func(server command.Server) {
defer func() {
if err := recover(); err != nil {
fmt.Printf(console.ColorfulText(console.TextRed, "Error: %s\n"), err)
}
}()
defer wg.Done()
// If there is no tail_file for a service configuration, the global configuration is used
if server.TailFile == "" {
server.TailFile = config.TailFile
}
// If the service configuration does not have a port, the default value of 22 is used
if server.Port == 0 {
server.Port = 22
}
cmd := command.NewCommand(server)
cmd.Execute(outputs)
}(server)
}
if len(config.Servers) > 0 {
go func() {
for output := range outputs {
fmt.Printf(
"%s %s %s",
console.ColorfulText(console.TextGreen, output.Host),
console.ColorfulText(console.TextYellow, "->"),
output.Content,
)
}
}()
} else {
fmt.Println(console.ColorfulText(console.TextRed, "No target host is available"))
}
wg.Wait()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。