Remote Terminal
Execute commands on remote Linux servers through SSH, Telnet, or web terminals. Supports password authentication, SSH keys, and SSH config aliases.
Quick Start
Basic SSH Connection
CODEBLOCK0
Using SSH Config Aliases
If the user has ~/.ssh/config configured:
CODEBLOCK1
With Password (using sshpass)
CODEBLOCK2
Connection Methods
1. SSH (Recommended)
Key-based authentication (most secure):
CODEBLOCK3
Using SSH config aliases:
CODEBLOCK4
Password authentication:
CODEBLOCK5
2. Telnet
CODEBLOCK6
3. Web Terminal (ttyd, wetty)
For web-based terminals, use curl or HTTP requests to the terminal's API:
CODEBLOCK7
Security Features
Command Confirmation
Before executing dangerous commands, ask the user to confirm:
Dangerous command patterns:
- -
rm -rf, rm -r, del, INLINECODE4 - INLINECODE5 ,
reboot, poweroff, INLINECODE8 - INLINECODE9 ,
fdisk, parted, INLINECODE12 - INLINECODE13 , INLINECODE14
- INLINECODE15 , INLINECODE16
- INLINECODE17 ,
pkill, INLINECODE19 - INLINECODE20 ,
ufw, INLINECODE22 - INLINECODE23 ,
DELETE FROM, INLINECODE25
Confirmation format:
⚠️ Dangerous command detected: rm -rf /var/log/*
This will permanently delete files. Proceed? (yes/no)
Command Blacklist
These commands are blocked by default and require explicit user override:
- -
rm -rf / (entire filesystem) - INLINECODE28 on mounted drives
- INLINECODE29 to primary disk
- Any command piping to
/dev/sda or similar
Operation Logging
All remote commands are logged with timestamp, target host, and command:
CODEBLOCK8
Log location: INLINECODE31
Workflow
Step 1: Identify Target Host
Parse the user's request to identify:
- - Hostname, IP address, or SSH alias
- Username (if specified, otherwise use default or prompt)
- Connection method (SSH by default)
Example prompts:
- - "Connect to production and run docker ps" → alias: production
- "SSH to 192.168.1.50, check disk space" → host: 192.168.1.50
- "On my server, restart nginx" → need to ask which server
Step 2: Build Connection Command
Construct the appropriate SSH command based on:
- - Authentication method available
- Host configuration
- Whether it's interactive or one-shot
Step 3: Security Check
If command matches dangerous patterns:
- 1. Warn the user
- Ask for explicit confirmation
- If confirmed, proceed; otherwise, cancel
Step 4: Execute and Return Output
Run the command and return:
- - Standard output
- Standard error (if any)
- Exit code
- Execution time
Step 5: Log Operation
Record the operation in the log file for audit trail.
Common Operations
Check System Status
CODEBLOCK9
Docker Management
CODEBLOCK10
Service Management
CODEBLOCK11
File Operations
CODEBLOCK12
Process Management
CODEBLOCK13
Interactive Sessions
For commands requiring interaction, use ssh -t for pseudo-terminal:
CODEBLOCK14
Note: Interactive sessions require the -t flag to allocate a PTY.
Multiple Hosts
Parallel Execution
Execute the same command on multiple hosts:
CODEBLOCK15
Using Parallel SSH
For larger fleets:
CODEBLOCK16
Host Management
Store Host Information
Hosts can be stored in ~/.qclaw/workspace/memory/hosts.json:
CODEBLOCK17
List Known Hosts
CODEBLOCK18
Troubleshooting
Connection Refused
CODEBLOCK19
Permission Denied
CODEBLOCK20
Host Key Verification Failed
CODEBLOCK21
Output Parsing
Structured Output
For commands returning JSON:
CODEBLOCK22
Table Output
For commands like docker ps, ps aux:
CODEBLOCK23
Resources
scripts/
- -
ssh_exec.py - Python wrapper for SSH operations with logging - INLINECODE38 - Manage host configurations
- INLINECODE39 - Execute commands on multiple hosts
references/
- -
ssh_config_guide.md - SSH config file examples and patterns - INLINECODE41 - Security guidelines for remote access
远程终端
通过SSH、Telnet或Web终端在远程Linux服务器上执行命令。支持密码认证、SSH密钥和SSH配置别名。
快速开始
基本SSH连接
bash
ssh user@hostname command
使用SSH配置别名
如果用户已配置~/.ssh/config:
bash
ssh command
带密码连接(使用sshpass)
bash
sshpass -p password ssh user@hostname command
连接方式
1. SSH(推荐)
基于密钥认证(最安全):
bash
ssh -i ~/.ssh/id_rsa user@hostname command
使用SSH配置别名:
bash
示例 ~/.ssh/config
Host production
HostName 192.168.1.100
User admin
Port 22
IdentityFile ~/.ssh/id_rsa
使用方式
ssh production docker ps
密码认证:
bash
sshpass -p password ssh -o StrictHostKeyChecking=no user@hostname command
2. Telnet
bash
使用expect进行交互式telnet
expect -c
spawn telnet hostname
expect login:
send username\r
expect Password:
send password\r
expect $
send command\r
expect $
send exit\r
3. Web终端(ttyd、wetty)
对于基于Web的终端,使用curl或HTTP请求访问终端API:
bash
示例:ttyd WebSocket连接(需要wscat或类似工具)
wscat -c ws://hostname:7681/ws
安全特性
命令确认
在执行危险命令前,要求用户确认:
危险命令模式:
- - rm -rf、rm -r、del、erase
- shutdown、reboot、poweroff、halt
- mkfs、fdisk、parted、dd
- chmod 777、chown -R
- > /dev/、truncate
- kill -9、pkill、killall
- iptables、ufw、firewall-cmd
- DROP DATABASE、DELETE FROM、TRUNCATE
确认格式:
⚠️ 检测到危险命令:rm -rf /var/log/*
这将永久删除文件。是否继续?(yes/no)
命令黑名单
以下命令默认被阻止,需要用户明确覆盖:
- - rm -rf /(整个文件系统)
- 在已挂载驱动器上执行mkfs
- 对主磁盘执行dd
- 任何管道到/dev/sda或类似设备的命令
操作日志
所有远程命令都会记录时间戳、目标主机和命令:
[2026-03-21 15:30:45] [production] docker ps
[2026-03-21 15:31:02] [staging] systemctl restart nginx
日志位置:~/.qclaw/logs/remote-terminal.log
工作流程
步骤1:识别目标主机
解析用户请求以识别:
- - 主机名、IP地址或SSH别名
- 用户名(如果指定,否则使用默认值或提示输入)
- 连接方式(默认为SSH)
示例提示:
- - 连接到生产环境并运行docker ps → 别名:production
- SSH到192.168.1.50,检查磁盘空间 → 主机:192.168.1.50
- 在我的服务器上重启nginx → 需要询问具体服务器
步骤2:构建连接命令
基于以下条件构建适当的SSH命令:
步骤3:安全检查
如果命令匹配危险模式:
- 1. 警告用户
- 要求明确确认
- 如果确认,继续执行;否则取消
步骤4:执行并返回输出
运行命令并返回:
步骤5:记录操作
将操作记录到日志文件中,用于审计追踪。
常见操作
检查系统状态
bash
ssh host uptime && free -h && df -h
Docker管理
bash
ssh host docker ps -a
ssh host docker logs container_name
ssh host docker restart container_name
服务管理
bash
ssh host systemctl status nginx
ssh host sudo systemctl restart nginx
ssh host journalctl -u nginx -f --no-pager -n 50
文件操作
bash
查看文件
ssh host cat /var/log/nginx/error.log | tail -50
复制文件到本地
scp user@host:/remote/path /local/path
复制文件到远程
scp /local/path user@host:/remote/path
进程管理
bash
ssh host ps aux | grep nginx
ssh host top -b -n 1 | head -20
交互式会话
对于需要交互的命令,使用ssh -t分配伪终端:
bash
ssh -t host sudo nano /etc/nginx/nginx.conf
ssh -t host htop
注意: 交互式会话需要-t标志来分配PTY。
多主机操作
并行执行
在多个主机上执行相同命令:
bash
for host in web1 web2 web3; do
echo === $host ===
ssh $host uptime
done
使用并行SSH
适用于大规模主机群:
bash
使用pssh(parallel-ssh)
pssh -h hosts.txt uptime
hosts.txt格式
web1.example.com
web2.example.com
web3.example.com
主机管理
存储主机信息
主机信息可存储在~/.qclaw/workspace/memory/hosts.json:
json
{
hosts: {
production: {
host: 192.168.1.100,
user: admin,
method: ssh-key,
key: ~/.ssh/id_rsa,
tags: [web, critical]
},
staging: {
host: staging.example.com,
user: deploy,
method: ssh-config,
alias: staging,
tags: [web, testing]
}
}
}
列出已知主机
bash
从SSH配置中获取
grep ^Host ~/.ssh/config | awk {print $2}
从存储的hosts.json中获取
cat ~/.qclaw/workspace/memory/hosts.json
故障排除
连接被拒绝
bash
检查主机是否可达
ping hostname
检查SSH端口是否开放
nc -zv hostname 22
使用详细输出尝试连接
ssh -vvv user@hostname
权限被拒绝
bash
检查密钥权限
chmod 600 ~/.ssh/id_rsa
使用特定密钥尝试
ssh -i ~/.ssh/id_rsa user@hostname
检查密钥是否已添加到代理
ssh-add -l
ssh-add ~/.ssh/id_rsa
主机密钥验证失败
bash
移除旧的主机密钥
ssh-keygen -R hostname
或临时禁用检查(生产环境不推荐)
ssh -o StrictHostKeyChecking=no user@hostname
输出解析
结构化输出
对于返回JSON的命令:
bash
ssh host docker inspect container --format {{json .}} | jq .
表格输出
对于docker ps、ps aux等命令:
bash
原样返回可读表格
ssh host docker ps --format table {{.Names}}\t{{.Status}}
解析为结构化数据
ssh host docker ps --format {{json .}} | jq .
资源
scripts/
- - sshexec.py - SSH操作的Python封装,带日志记录
- hostmanager.py - 管理主机配置
- parallel_exec.py - 在多台主机上执行命令
references/
- - sshconfigguide.md - SSH配置文件示例和模式
- securitybestpractices.md - 远程访问安全指南