Last active
January 30, 2023 01:41
-
-
Save blacknon/b9b70aa739c169eb9c2f886055ee8ba7 to your computer and use it in GitHub Desktop.
Goでターミナルログを記録するサンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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