实时输出, 自定义回话输入
源自:51证明网
func test4() {
cmd := exec.Command("openssl")
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
stdin, _ := cmd.StdinPipe()
cmd.Start()
// 读
var wg sync.WaitGroup
wg.Add(3)
go func() {
defer wg.Done()
for {
buf := make([]byte, 1024)
n, err := stderr.Read(buf)
if n > 0 {
fmt.Println(string(buf[:n]))
}
if n == 0 {
break
}
if err != nil {
log.Printf("read err %v", err)
return
}
}
}()
go func() {
defer wg.Done()
for {
buf := make([]byte, 1024)
n, err := stdout.Read(buf)
if n == 0 {
break
}
if n > 0 {
fmt.Println(string(buf[:n]))
}
if n == 0 {
break
}
if err != nil {
log.Printf("read out %v", err)
return
}
}
}()
// 写
go func() {
stdin.Write([]byte("version\n\n"))
stdin.Write([]byte("ciphers -v\n\n"))
stdin.Write([]byte("s_client -connect razeencheng.com:443"))
stdin.Close()
wg.Done()
}()
wg.Wait()
err := cmd.Wait()
if err != nil {
log.Printf("cmd wait %v", err)
return
}
}```