duanpei4455 2017-11-02 02:57
浏览 152

使用GoLang自动化mysql_secure_installation

The third command RunCommand("mysql_secure_installation"); doesn't show stdout/stderr buffers and command will not finish. Keyboard typing works but doesn't affect the process.

The mysql_secure_installation on ssh console works perfect.

Other commands work perfectly.

package main

import (
    "fmt"
    "os/exec"
    "os"
    "bufio"
)

func main() {
    RunCommand("lsb_release -a"); //works perfect
    RunCommand("apt-get update"); //works perfect
    RunCommand("mysql_secure_installation"); //empty output and waiting for something!
}

func RunCommand(command string) {
    args := []string {"-c", command}
    executer := exec.Command("sh", args...)

    stdout, err := executer.StdoutPipe()
    if err != nil {
        fmt.Print("Error creating STDOUT pipe")
        os.Exit(1)
    }

    stderr, err := executer.StderrPipe()
    if err != nil {
        fmt.Print("Error creating STDERR pipe")
        os.Exit(1)
    }

    stdoutScanner := bufio.NewScanner(stdout)
    stdoutScanner.Split(bufio.ScanLines)
    go func() {
        for stdoutScanner.Scan() {
            out := stdoutScanner.Text();
            fmt.Printf("%s
", out)
        }
    }()

    stderrScanner := bufio.NewScanner(stderr)
    stderrScanner.Split(bufio.ScanLines)
    go func() {
        for stderrScanner.Scan() {
            error := stderrScanner.Text()
            fmt.Printf("%s
", error)
        }
    }()

    err = executer.Start()
    if err != nil {
        os.Exit(1)
    }

    err = executer.Wait()
    if err != nil {
        os.Exit(1)
    }
}

UPDATE 1:

The main problem found and asked as new question in this link: How to store STDOUT buffer of `mysql_secure_installation` to a file

UPDATE 2:

CentOS and Debian tested and buffer works perfect but on my target os ( Ubuntu 16.04 LTS ) it doesn't work.

  • 写回答

1条回答 默认 最新

  • doutou3725 2017-11-02 07:43
    关注

    The reason it hangs is because it is still waiting for user input to complete.

    As mentioned in the comments, mysql_secure_installation requires user input. If you want to run it without user input, you can try adding the --use-default argument.

    If you want to wait for use input, then consider reading up on the difference between Run and Start. From the documentation:

    Run starts the specified command and waits for it to complete.

    Start starts the specified command but does not wait for it to complete.

    You may need to experiment with using Run to allow user input to the program. Alternately, you could probably redirect another string into the command using stdin, but that might be more complicated than it's worth.

    评论

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写