diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2018-07-06 13:49:40 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2018-07-06 14:02:06 +0200 |
| commit | 8c2335a205682fbbe311fb5ad4393419fd094a99 (patch) | |
| tree | 3a2cc018d048677a663eba6bd0b240c408b0ac80 /vm/vmimpl | |
| parent | 2eaf564197e0707cd172e8111fdabb7e471ec4a8 (diff) | |
vm/vmimpl: factor out common code for ssh args and waiting for ssh
Move common code from 4 vm implementations to vmimpl.
Diffstat (limited to 'vm/vmimpl')
| -rw-r--r-- | vm/vmimpl/util.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/vm/vmimpl/util.go b/vm/vmimpl/util.go index f80de8ab1..08034450f 100644 --- a/vm/vmimpl/util.go +++ b/vm/vmimpl/util.go @@ -4,7 +4,11 @@ package vmimpl import ( + "fmt" "time" + + "github.com/google/syzkaller/pkg/log" + "github.com/google/syzkaller/pkg/osutil" ) // Sleep for d. @@ -17,3 +21,56 @@ func SleepInterruptible(d time.Duration) bool { return false } } + +func WaitForSSH(debug bool, timeout time.Duration, addr, sshKey, sshUser, OS string, port int) error { + pwd := "pwd" + if OS == "windows" { + pwd = "dir" + } + startTime := time.Now() + SleepInterruptible(5 * time.Second) + for { + if !SleepInterruptible(5 * time.Second) { + return fmt.Errorf("shutdown in progress") + } + args := append(SSHArgs(debug, sshKey, port), sshUser+"@"+addr, pwd) + if debug { + log.Logf(0, "running ssh: %#v", args) + } + _, err := osutil.RunCmd(time.Minute, "", "ssh", args...) + if err == nil { + return nil + } + if time.Since(startTime) > timeout { + return fmt.Errorf("can't ssh into the instance: %v", err) + } + } +} + +func SSHArgs(debug bool, sshKey string, port int) []string { + return sshArgs(debug, sshKey, "-p", port) +} + +func SCPArgs(debug bool, sshKey string, port int) []string { + return sshArgs(debug, sshKey, "-P", port) +} + +func sshArgs(debug bool, sshKey, portArg string, port int) []string { + args := []string{ + portArg, fmt.Sprint(port), + "-i", sshKey, + "-F", "/dev/null", + "-o", "UserKnownHostsFile=/dev/null", + "-o", "BatchMode=yes", + "-o", "IdentitiesOnly=yes", + "-o", "StrictHostKeyChecking=no", + "-o", "ConnectTimeout=10", + } + if sshKey != "" { + args = append(args, "-i", sshKey) + } + if debug { + args = append(args, "-v") + } + return args +} |
