tasklist 遠端獲取程式報錯賬號密碼錯誤

snowlyg發表於2021-02-05

tasklist 遠端獲取程式報錯賬號密碼錯誤

問題描述:寫了一個監控程式,需要遠端獲取 windows 系統下某個程式是否在執行?因為業務需要這個程式需要註冊為 windows 服務(如果直接命令列執行,可能其他人會誤操作推出程式,很多人同時會用這伺服器)。

服務註冊用的是 github.com/kardianos/service 包。

執行命令的部分程式碼
args := []string{"/C", "tasklist", "/s", "127.0.0.1", "/u", "Administrator", "/p", "password", "/fi", "IMAGENAME eq ****.exe"}
            cmd := exec.Command("cmd.exe", args...)
            logger.Info(fmt.Sprintf("%+v", cmd))
            var out bytes.Buffer
            var outErr bytes.Buffer
            cmd.Stdout = &out
            cmd.Stderr = &outErr
            if err := cmd.Run(); err != nil {
            // 如果註冊成服務,這裡返回的資料會亂碼,返回的是GBK格式。
                utf8Data := outErr.Bytes()
                if isGBK(outErr.Bytes()) {
                    utf8Data, _ = simplifiedchinese.GBK.NewDecoder().Bytes(outErr.Bytes())
                    logger.Info(fmt.Sprintf("outErr isUtf8 %v", isUtf8(utf8Data)))
                }
                logger.Info(fmt.Sprintf("outErr isGBK %v", isGBK(outErr.Bytes())))
                logger.Info(fmt.Sprintf("outErr isUtf8 %v", isUtf8(outErr.Bytes())))
                logger.Info(fmt.Sprintf("tasklist %v", string(utf8Data)))
            }
            logger.Info(fmt.Sprintf("out isGBK %v", isGBK(out.Bytes())))
            logger.Info(fmt.Sprintf("out isUtf8 %v", isUtf8(out.Bytes())))
            logger.Info(fmt.Sprintf("%v", out.String()))
            logger.Info("info app count", zap.Int("count:", strings.Count(out.String(), "*****.exe")))

返回資訊大致是這樣,亂碼部分轉碼後為賬號或密碼錯誤。
tasklist 遠端獲取程式報錯賬號密碼錯誤

明明賬號密碼都沒問題,直接命令列執行都是正常的,這情況真的是有點摸不著頭腦。
看了一遍 github.com/kardianos/service 的原始碼,發現註冊服務是可以設定服務的執行使用者的。


// Config provides the setup for a Service. The Name field is required.
type Config struct {
    Name        string   // Required name of the service. No spaces suggested.
    DisplayName string   // Display name, spaces allowed.
    Description string   // Long description of service.
    UserName    string   // Run as username.
    Arguments   []string // Run with arguments.

    // Optional field to specify the executable for service.
    // If empty the current executable is used.
    Executable string

    // Array of service dependencies.
    // Not yet fully implemented on Linux or OS X:
    //  1. Support linux-systemd dependencies, just put each full line as the
    //     element of the string array, such as
    //     "After=network.target syslog.target"
    //     "Requires=syslog.target"
    //     Note, such lines will be directly appended into the [Unit] of
    //     the generated service config file, will not check their correctness.
    Dependencies []string

    // The following fields are not supported on Windows.
    WorkingDirectory string // Initial working directory.
    ChRoot           string

    // System specific options.
    //  * OS X
    //    - LaunchdConfig string ()      - Use custom launchd config
    //    - KeepAlive     bool   (true)
    //    - RunAtLoad     bool   (false)
    //    - UserService   bool   (false) - Install as a current user service.
    //    - SessionCreate bool   (false) - Create a full user session.
    //  * POSIX
    //    - SystemdScript string ()                 - Use custom systemd script
    //    - UpstartScript string ()                 - Use custom upstart script
    //    - SysvScript    string ()                 - Use custom sysv script
    //    - RunWait       func() (wait for SIGNAL)  - Do not install signal but wait for this function to return.
    //    - ReloadSignal  string () [USR1, ...]     - Signal to send on reaload.
    //    - PIDFile       string () [/run/prog.pid] - Location of the PID file.
    //    - LogOutput     bool   (false)            - Redirect StdErr & StandardOutPath to files.
    //    - Restart       string (always)           - How shall service be restarted.
    //    - SuccessExitStatus string ()             - The list of exit status that shall be considered as successful,
    //                                                in addition to the default ones.
    //  * Linux (systemd)
    //    - LimitNOFILE     int - Maximum open files (ulimit -n) (https://serverfault.com/questions/628610/increasing-nproc-for-processes-launched-by-systemd-on-centos-7)
    //  * Windows
    //    - DelayedAutoStart  bool (false) - after booting start this service after some delay

    Option KeyValue
}

經過嘗試之後,賬號密碼還是沒有生效。在 issues 裡面,也沒找到什麼有用的資訊。

正在我一籌莫展之際,我突然想起 windows 服務在註冊好之後是可以設定賬號密碼的:

tasklist 遠端獲取程式報錯賬號密碼錯誤

一頓操作重啟服務,果然服務可以正常執行了。

問題解決,今天又是愉快的一天!!

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章