Skip to content

Commit

Permalink
update tssh images and document
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Jan 27, 2024
1 parent f5e1419 commit ecf2f40
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 37 deletions.
61 changes: 43 additions & 18 deletions cn/ssh.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,15 @@ trzsz-ssh ( tssh ) 设计为 ssh 客户端的直接替代品,提供与 openssh

- 按下 `Space``Ctrl+X` 等可以选中或取消当前服务器,若不能选中说明还不支持当前终端,请先运行 `tmux`

- 按下 `Ctrl+P` 会以分屏的方式批量登录,`Ctrl+W` 会打开新窗口批量登录,`Ctrl+T` 会打开新 tab 批量登录。
- 按下 `a``Ctrl+A` 全选当前页所有机器,`o``Ctrl+O` 反选当前页所有机器,`d``l` 翻到下一页。

- 按下 `p``Ctrl+P` 以分屏的方式登录,`w``Ctrl+W` 以新窗口登录,`t``Ctrl+T` 以新 tab 登录。

- `tssh` 不带参数启动可以批量登录服务器,若带 `-o RemoteCommand` 参数启动则可以批量执行指定的命令。支持执行指定命令之后进入交互式 shell,但 `Windows Terminal` 不支持分号 `;`,可以用 `|cat&&` 代替。举例:

```sh
tssh -t -o RemoteCommand='ping -c3 trzsz.github.io ; bash'
tssh -t -o RemoteCommand="ping -c3 trzsz.github.io |cat&& bash"
tssh -t -o RemoteCommand='ping -c3 trzsz.github.io ; bash -l'
tssh -t -o RemoteCommand="ping -c3 trzsz.github.io |cat&& bash -l"
```

![tssh batch](https://trzsz.github.io/images/tssh_batch.gif)
Expand Down Expand Up @@ -380,34 +382,57 @@ trzsz-ssh ( tssh ) 设计为 ssh 客户端的直接替代品,提供与 openssh

## 自动交互

- 支持类似 `expect` 的自动交互功能,可以在登录服务器之后,自动匹配服务器的输出,然后自动输入。
- 支持类似 `expect` 的自动交互功能,在登录服务器之后,自动匹配服务器的输出,然后自动输入。

```
Host auto
#!! ExpectCount 5 # 配置自动交互的次数,默认是 0 即无自动交互
#!! ExpectCount 2 # 配置自动交互的次数,默认是 0 即无自动交互
#!! ExpectTimeout 30 # 配置自动交互的超时时间(单位:秒),默认是 30 秒
#!! ExpectPattern1 *assword # 配置第一个自动交互的匹配表达式
# 配置第一个自动输入(密文),这是由 tssh --enc-secret 编码得到的字符串,tssh 会自动发送 \r 回车
#!! ExpectSendPass1 d7983b4a8ac204bd073ed04741913befd4fbf813ad405d7404cb7d779536f8b87e71106d7780b2
#!! ExpectPattern2 hostname*$ # 配置第二个自动交互的匹配表达式
#!! ExpectSendText2 echo tssh expect\r # 配置第二个自动输入(明文),需要指定 \r 才会发送回车
# 以上 ExpectSendPass? 和 ExpectSendText? 只要二选一即可,若都配置则 ExpectSendPass? 的优先级更高
# --------------------------------------------------
# 在每个 ExpectPattern 匹配之前,可以配置一个或多个可选的匹配,用法如下:
#!! ExpectPattern3 hostname*$ # 配置第三个自动交互的匹配表达式
#!! ExpectSendText3 ssh xxx\r # 配置第三个自动输入,也可以换成 ExpectSendPass3 然后配置密文
#!! ExpectCaseSendText3 yes/no y\r # 在 ExpectPattern3 匹配之前,若遇到 yes/no 则发送 y 并回车
#!! ExpectCaseSendText3 y/n yes\r # 在 ExpectPattern3 匹配之前,若遇到 y/n 则发送 yes 并回车
#!! ExpectCaseSendPass3 token d7... # 在 ExpectPattern3 匹配之前,若遇到 token 则解码 d7... 并发送
# --------------------------------------------------
#!! ExpectPattern4 token: # 配置第四个自动交互的匹配表达式(这里以动态密码举例)
#!! ExpectSendOtp4 oathtool --totp -b xxxxx # 配置获取动态密码的命令(明文)
#!! ExpectPattern5 token: # 配置第五个自动交互的匹配表达式(这里以动态密码举例)
```

- 在每个 `ExpectPattern?` 匹配之前,如果遇到可选的匹配则自动输入,用法如下:

```
Host case
#!! ExpectCount 1 # 配置自动交互的次数,默认是 0 即无自动交互
#!! ExpectPattern1 hostname*$ # 配置第一个自动交互的匹配表达式
#!! ExpectSendText1 ssh xxx\r # 配置第一个自动输入,也可以换成 ExpectSendPass1 然后配置密文
#!! ExpectCaseSendText1 yes/no y\r # 在 ExpectPattern1 匹配之前,若遇到 yes/no 则发送 y 并回车
#!! ExpectCaseSendText1 y/n yes\r # 在 ExpectPattern1 匹配之前,若遇到 y/n 则发送 yes 并回车
#!! ExpectCaseSendPass1 token d7... # 在 ExpectPattern1 匹配之前,若遇到 token 则解码 d7... 并发送
```

- 在匹配到指定输出时,执行指定的命令获取动态密码,然后自动输入,用法如下:

```
Host otp
#!! ExpectCount 2 # 配置自动交互的次数,默认是 0 即无自动交互
#!! ExpectPattern1 token: # 配置第一个自动交互的匹配表达式
#!! ExpectSendOtp1 oathtool --totp -b xxxxx # 配置获取动态密码的命令(明文)
#!! ExpectPattern2 token: # 配置第二个自动交互的匹配表达式
# 下面是运行 tssh --enc-secret 输入命令 oathtool --totp -b xxxxx 得到的密文串
#!! ExpectSendEncOtp5 77b4ce85d087b39909e563efb165659b22b9ea700a537f1258bdf56ce6fdd6ea70bc7591ea5c01918537a65433133bc0bd5ed3e4
#!! ExpectSendEncOtp2 77b4ce85d087b39909e563efb165659b22b9ea700a537f1258bdf56ce6fdd6ea70bc7591ea5c01918537a65433133bc0bd5ed3e4
```

- 可能有些服务器不支持连着发送数据,如输入 `1\r`,要求在 `1` 之后有一点延迟,然后再 `\r` 回车,则可以用 `\|` 间开。

```
Host sleep
#!! ExpectCount 2 # 配置自动交互的次数,默认是 0 即无自动交互
#!! ExpectSleepMS 100 # 当要间开输入时,sleep 的毫秒数,默认 100ms
#!! ExpectPattern1 x> # 配置第一个自动交互的匹配表达式
#!! ExpectSendText1 1\|\r # 配置第一个自动输入,在发送 1 之后,先 sleep 100ms,再发送 \r 回车
#!! ExpectPattern2 y> # 配置第二个自动交互的匹配表达式
#!! ExpectSendText2 \|1\|\|\r # 先 sleep 100ms,然后发送 1,再 sleep 200ms,最后发送 \r 回车
```

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

## 记住密码

Expand Down
2 changes: 1 addition & 1 deletion images/tssh.gif
Binary file modified images/tssh_batch.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/tssh_simple.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/tssh_table.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/tssh_tiny.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 43 additions & 18 deletions ssh.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,17 @@ trzsz-ssh ( tssh ) is an ssh client designed as a drop-in replacement for the op

- tssh supports selecting multiple servers in `iTerm2`( Requires [Python API](https://iterm2.com/python-api-auth.html), no need to `Allow all apps to connect` ),`tmux` and `Windows Terminal`, logging in to them in batches, and executing pre-specified commands in batches.

- Press `Space`, `Ctrl+X` to toggle select the current server. If it cannot be selected, it means that the current terminal is not supported yet. Please run `tmux` first.
- Press `Space`, `x` or `Ctrl+X` to toggle select the current host. If it cann't be selected, it means that the current terminal is not supported yet. Please run `tmux` first.
- Press `Ctrl+P` will split panes for batch login, `Ctrl+W` will open new windows for batch login, and `Ctrl+T` will open new tabs for batch login.
- Press `a` or `Ctrl+A` to select all the hosts on the current page, `o` or `Ctrl+O` to select the opposite hosts on the current page, `d` or `l` move to next page.
- Press `p` or `Ctrl+P` will split panes for batch login, `w` or `Ctrl+W` will open new windows for batch login, `t` or `Ctrl+T` will open new tabs for batch login.
- Execute the `tssh` command without arguments, you can log in to servers in batches. And you can specify the commands to be executed in batches by `-o RemoteCommand`. And you can switch to an interactive shell after executing the specified command. `Windows Terminal` does not support semicolon `;`, you can use `|cat&&` instead. For example:
```sh
tssh -t -o RemoteCommand='ping -c3 trzsz.github.io ; bash'
tssh -t -o RemoteCommand="ping -c3 trzsz.github.io |cat&& bash"
tssh -t -o RemoteCommand='ping -c3 trzsz.github.io ; bash -l'
tssh -t -o RemoteCommand="ping -c3 trzsz.github.io |cat&& bash -l"
```
![tssh batch](https://trzsz.github.io/images/tssh_batch.gif)
Expand Down Expand Up @@ -384,30 +386,53 @@ trzsz-ssh ( tssh ) is an ssh client designed as a drop-in replacement for the op

```
Host auto
#!! ExpectCount 5 # Configures the number of automated interactions, default is 0 which means no automated interaction
#!! ExpectCount 2 # Configures the number of automated interactions, default is 0 which means no automated interaction
#!! ExpectTimeout 30 # Configures the timeout for automated interaction (in seconds), default is 30 seconds
#!! ExpectPattern1 *assword # Configures the first automated interaction match expression
# Configures the first automated input (encrypted). It was encoded by `tssh --enc-secret`, `tssh` will send \r (enter) automatically
#!! ExpectSendPass1 d7983b4a8ac204bd073ed04741913befd4fbf813ad405d7404cb7d779536f8b87e71106d7780b2
#!! ExpectPattern2 hostname*$ # Configures the second automated interaction match expression
#!! ExpectSendText2 echo tssh expect\r # Configures the second automated input (plaintext), specify \r to send enter
# Choose either ExpectSendPass? or ExpectSendText? for each interaction; if both are configured, ExpectSendPass? has higher priority
# --------------------------------------------------
# Before each ExpectPattern match, one or multiple optional matches can be configured as follows:
#!! ExpectPattern3 hostname*$ # Configures the third automated interaction match expression
#!! ExpectSendText3 ssh xxx\r # Configures the third automated input, can also use ExpectSendPass3 then configure with encrypted text
#!! ExpectCaseSendText3 yes/no y\r # Before matching ExpectPattern3, if encountering yes/no, then send y and enter
#!! ExpectCaseSendText3 y/n yes\r # Before matching ExpectPattern3, if encountering y/n, then send yes and enter
#!! ExpectCaseSendPass3 token d7... # Before matching ExpectPattern3, if encountering token, then decode d7... and send
# --------------------------------------------------
#!! ExpectPattern4 token: # Configures the fourth automated interaction match expression (one-time password)
#!! ExpectSendOtp4 oathtool --totp -b xxxxx # Configure the command line to obtain the one-time password
#!! ExpectPattern5 token: # Configures the fifth automated interaction match expression (one-time password)
```

- Before each `ExpectPattern?` match, one or multiple optional matches can be configured as follows:

```
Host case
#!! ExpectCount 1 # Configures the number of automated interactions, default is 0 which means no automated interaction
#!! ExpectPattern1 hostname*$ # Configures the first automated interaction match expression
#!! ExpectSendText1 ssh xxx\r # Configures the first automated input, can also use ExpectSendPass1 then configure with encrypted text
#!! ExpectCaseSendText1 yes/no y\r # Before matching ExpectPattern1, if encountering yes/no, then send y and enter
#!! ExpectCaseSendText1 y/n yes\r # Before matching ExpectPattern1, if encountering y/n, then send yes and enter
#!! ExpectCaseSendPass1 token d7... # Before matching ExpectPattern1, if encountering token, then decode d7... and send
```
- When the server's output is matched, execute the specified command to obtain the one-time password, and send it:
```
Host otp
#!! ExpectCount 2 # Configures the number of automated interactions, default is 0 which means no automated interaction
#!! ExpectPattern1 token: # Configures the first automated interaction match expression
#!! ExpectSendOtp1 oathtool --totp -b xxxxx # Configure the command line to obtain the one-time password
#!! ExpectPattern2 token: # Configures the second automated interaction match expression
# The following ciphertext was generated by encoding `oathtool --totp -b xxxxx` with `tssh --enc-secret`.
#!! ExpectSendEncOtp5 77b4ce85d087b39909e563efb165659b22b9ea700a537f1258bdf56ce6fdd6ea70bc7591ea5c01918537a65433133bc0bd5ed3e4
#!! ExpectSendEncOtp2 77b4ce85d087b39909e563efb165659b22b9ea700a537f1258bdf56ce6fdd6ea70bc7591ea5c01918537a65433133bc0bd5ed3e4
```
- Some servers may not support sending data continuously. For example, sending `1\r` requires a slight delay after `1` before sending `\r`. You can use `\|` to separate them.
```
Host sleep
#!! ExpectCount 2 # Configures the number of automated interactions, default is 0 which means no automated interaction
#!! ExpectSleepMS 100 # Number of milliseconds to sleep between separate inputs, default is 100ms
#!! ExpectPattern1 x> # Configures the first automated interaction match expression
#!! ExpectSendText1 1\|\r # Configures the first automated input, send 1, sleep 100ms, send \r.
#!! ExpectPattern2 y> # Configures the second automated interaction match expression
#!! ExpectSendText2 \|1\|\|\r # Configures the second automated input, sleep 100ms, send 1, sleep 200ms, send \r.
```
- Login using `tssh --debug` if `ExpectCount` is greater than `0`, you can see the output captured by `expect`.
- 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

0 comments on commit ecf2f40

Please sign in to comment.