Skip to content

Commit

Permalink
support expect pass sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Feb 3, 2024
1 parent b021858 commit 5842303
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ trzsz-ssh ( tssh ) 设计为 ssh 客户端的直接替代品,提供与 openssh
#!! ExpectSendText2 \|1\|\|\r # 先 sleep 100ms,然后发送 1,再 sleep 200ms,最后发送 \r 回车
```

- 有些服务器连密码也不支持连着发送,则需要配置 `ExpectPassSleep`,默认为 `no`,可配置为 `each``enter`

- 配置 `ExpectPassSleep each` 则每输入一个字符就 sleep 一小段时间,默认 100 毫秒,可配置 `ExpectSleepMS` 进行调整。
- 配置 `ExpectPassSleep enter` 则只是在发送 `\r` 回车之前 sleep 一小段时间,默认 100 毫秒,可配置 `ExpectSleepMS` 进行调整。

- 如果不知道 `ExpectPattern2` 如何配置,可以先将 `ExpectCount` 配置为 `2`,然后使用 `tssh --debug` 登录,就会看到 `expect` 捕获到的输出,可以直接复制输出的最后部分来配置 `ExpectPattern2`。把 `2` 换成其他任意的数字也适用。

## 记住密码
Expand Down
5 changes: 5 additions & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ trzsz-ssh ( tssh ) is an ssh client designed as a drop-in replacement for the op
#!! ExpectSendText2 \|1\|\|\r # Configures the second automated input, sleep 100ms, send 1, sleep 200ms, send \r.
```
- Some servers may not support sending password continuously. Then you need to configure `ExpectPassSleep`, which is `no` by default, and can be configured as `each` or `enter`:
- Configuring `ExpectPassSleep each` will sleep for a short period of time for each character send, the default is 100 milliseconds, and you can configure `ExpectSleepMS` to adjust it.
- Configuring `ExpectPassSleep enter` will only sleep for a short period of time before `\r` send, the default is 100 milliseconds, and you can configure `ExpectSleepMS` to adjust it.
- If you don’t know how to configure `ExpectPattern2`, you can first configure `ExpectCount` to `2`, then use `tssh --debug` to log in, you will see the output captured by `expect`, and you can directly copy the last part of the output to configure `ExpectPattern2`. Replacing `2` with any other number will also work.
## Remember Password
Expand Down
37 changes: 34 additions & 3 deletions tssh/expect.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ func (s *expectSender) decodeText(text string) [][]string {
return texts
}

func (s *expectSender) getExpectPsssSleep() (bool, bool) {
passSleep := getExConfig(s.expect.alias, fmt.Sprintf("%sExpectPassSleep", s.expect.pre))
switch strings.ToLower(passSleep) {
case "each":
return true, false
case "enter":
return false, true
default:
return false, false
}
}

func (s *expectSender) getExpectSleepTime() time.Duration {
expectSleepMS := getExConfig(s.expect.alias, fmt.Sprintf("%sExpectSleepMS", s.expect.pre))
if expectSleepMS == "" {
Expand All @@ -140,15 +152,34 @@ func (s *expectSender) sendInput(writer io.Writer, id string) bool {
warning("expect %s send nothing", id)
return true
}
var sleepTime time.Duration
if s.passwd {
debug("expect %s send: %s\\r", id, strings.Repeat("*", len(s.input)))
if err := writeAll(writer, []byte(s.input+"\r")); err != nil {
eachSleep, enterSleep := s.getExpectPsssSleep()
if eachSleep || enterSleep {
sleepTime = s.getExpectSleepTime()
}
for _, input := range []byte(s.input) {
debug("expect %s send: %s", id, "*")
if err := writeAll(writer, []byte{input}); err != nil {
warning("expect %s send input failed: %v", id, err)
return false
}
if eachSleep {
debug("expect %s sleep: %v", id, sleepTime)
time.Sleep(sleepTime)
}
}
if enterSleep {
debug("expect %s sleep: %v", id, sleepTime)
time.Sleep(sleepTime)
}
debug("expect %s send: \\r", id)
if err := writeAll(writer, []byte("\r")); err != nil {
warning("expect %s send input failed: %v", id, err)
return false
}
return true
}
var sleepTime time.Duration
for i, text := range s.decodeText(s.input) {
if i > 0 {
if i == 1 {
Expand Down

0 comments on commit 5842303

Please sign in to comment.