Skip to content

Instantly share code, notes, and snippets.

@blacknon
Last active January 30, 2023 01:41
Show Gist options
  • Save blacknon/b9b70aa739c169eb9c2f886055ee8ba7 to your computer and use it in GitHub Desktop.
Save blacknon/b9b70aa739c169eb9c2f886055ee8ba7 to your computer and use it in GitHub Desktop.
Goでターミナルログを記録するサンプルコード
package main
import (
"fmt"
"os"
"time"
"github.com/blacknon/gexpect"
)
func main() {
child, _ := gexpect.NewSubProcess("ssh", "-t", "blacknon@test-node", "-i", "/path/to/key")
if err := child.Start(); err != nil {
fmt.Println(err)
}
defer child.Close()
logPath := "/path/to/log"
wirteLog, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
fmt.Println(err)
}
defer wirteLog.Close()
child.Term.Log = wirteLog
child.InteractTimeout(2419200 * time.Second)
fmt.Println("finish!!!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment