tasklist 远程获取进程报错账号密码错误

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 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!