Add timeout to SSH command.

This commit is contained in:
Patrick Fairbank
2018-11-11 11:54:33 -08:00
parent d2db15b932
commit dc74b1dc19

View File

@@ -19,6 +19,7 @@ import (
const (
accessPointSshPort = 22
accessPointConnectTimeoutSec = 1
accessPointCommandTimeoutSec = 5
accessPointPollPeriodSec = 3
accessPointRequestBufferSize = 10
accessPointConfigRetryIntervalSec = 5
@@ -42,6 +43,11 @@ type TeamWifiStatus struct {
RadioLinked bool
}
type sshOutput struct {
output string
err error
}
func (ap *AccessPoint) SetSettings(address, username, password string, teamChannel, adminChannel int,
adminWpaKey string, networkSecurityEnabled bool) {
ap.address = address
@@ -210,8 +216,18 @@ func (ap *AccessPoint) runCommand(command string) (string, error) {
defer session.Close()
defer conn.Close()
outputBytes, err := session.Output(command)
return string(outputBytes), err
// Run the command with a timeout.
commandChan := make(chan sshOutput, 1)
go func() {
outputBytes, err := session.Output(command)
commandChan <- sshOutput{string(outputBytes), err}
}()
select {
case output := <-commandChan:
return output.output, output.err
case <-time.After(accessPointCommandTimeoutSec * time.Second):
return "", fmt.Errorf("WiFi SSH command timed out after %d seconds", accessPointCommandTimeoutSec)
}
}
// Verifies WPA key validity and produces the configuration command for the given list of teams.