2024-06-07 02:51:12 +00:00
|
|
|
package apcssh
|
|
|
|
|
|
|
|
import (
|
2024-06-19 23:56:17 +00:00
|
|
|
"io"
|
2024-06-07 02:51:12 +00:00
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
// scanAPCShell is a SplitFunc to capture shell output after each interactive
|
|
|
|
// shell command is run
|
|
|
|
func scanAPCShell(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
2024-06-19 23:56:17 +00:00
|
|
|
// EOF is not an expected response and should error (e.g., when the output pipe
|
|
|
|
// gets closed by timeout)
|
|
|
|
if atEOF {
|
|
|
|
return len(data), dropCR(data), io.ErrUnexpectedEOF
|
|
|
|
} else if len(data) == 0 {
|
|
|
|
// no data to process, request more data
|
2024-06-07 02:51:12 +00:00
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
2024-06-19 01:30:40 +00:00
|
|
|
// regex for shell prompt (e.g., `apc@apc>`, `apc>`, `some@dev>`, `other123>`, etc.)
|
|
|
|
re := regexp.MustCompile(`(\r\n|\r|\n)([A-Za-z0-9.]+@?)?[A-Za-z0-9.]+>`)
|
2024-06-07 02:51:12 +00:00
|
|
|
// find match for prompt
|
|
|
|
if index := re.FindStringIndex(string(data)); index != nil {
|
|
|
|
// advance starts after the prompt; token is everything before the prompt
|
|
|
|
return index[1], dropCR(data[0:index[0]]), nil
|
|
|
|
}
|
|
|
|
|
2024-06-19 23:56:17 +00:00
|
|
|
// no match, request more data
|
2024-06-07 02:51:12 +00:00
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// dropCR drops a terminal \r from the data.
|
|
|
|
func dropCR(data []byte) []byte {
|
|
|
|
if len(data) > 0 && data[len(data)-1] == '\r' {
|
|
|
|
return data[0 : len(data)-1]
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|