Windows 远程自动化部署 OpenClaw:从零到一的完整教程
·
## 前言
OpenClaw 是一个开源的 AI 助手平台,支持多模型、多渠道接入。本文将介绍如何通过 SSH 远程连接,在另一台 Windows 电脑上自动部署 OpenClaw。
**适用场景:**
- 多台电脑批量部署
- 远程服务器配置
- 不想手动敲命令的懒人
**技术栈:**
- Windows 10/11
- OpenSSH Server
- Node.js 24+
- OpenClaw 2026.7.1-2
- Python paramiko(SSH 客户端)
---
## 一、环境准备
### 1.1 目标机器配置(Windows 10)
**开启 OpenSSH Server:**
```powershell
# 以管理员身份运行 PowerShell
# 安装 OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
# 启动服务
Start-Service sshd
# 设置开机自启
Set-Service -Name sshd -StartupType Automatic
# 检查防火墙规则
Get-NetFirewallRule -Name *ssh*
```
**确认网络连通性:**
```powershell
# 查看本机 IP
ipconfig
# 假设 IP 为 192.168.3.93
```
### 1.2 主力机配置(已有 OpenClaw)
确保主力机已安装 OpenClaw,并配置好 AI 模型。
**测试 SSH 连接:**
```bash
# 在主力机上执行
ssh Administrator@192.168.3.93
# 输入密码,确认能登录
```
---
## 二、通过 OpenClaw 自动安装
### 2.1 建立连接
在 OpenClaw 对话中输入:
```
帮我用 SSH 连接到 192.168.3.93,用户名 Administrator,密码 023456
```
OpenClaw 会自动执行以下 Python 脚本:
```python
import paramiko
import sys
sys.stdout.reconfigure(encoding='utf-8')
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('192.168.3.93', username='Administrator', password='***', timeout=10)
# 测试连接
stdin, stdout, stderr = c.exec_command('echo connected && hostname')
print(stdout.read().decode('utf-8'))
c.close()
```
**输出:**
```
connected
DESKTOP-QL28LDF
```
### 2.2 安装 Node.js
检查 Node.js 版本:
```python
stdin, stdout, stderr = c.exec_command('node --version 2>&1')
out = stdout.read().decode('utf-8', 'replace')
print('Current node:', out.strip())
```
如果版本低于 22.22.3,需要升级:
```python
# 下载 Node.js 24 LTS
cmd = 'powershell -Command "Invoke-WebRequest -Uri https://nodejs.org/dist/v24.15.0/node-v24.15.0-x64.msi -OutFile C:\\Users\\Administrator\\node-v24.msi"'
stdin, stdout, stderr = c.exec_command(cmd)
stdout.read()
# 静默安装
cmd = 'msiexec /i C:\\Users\\Administrator\\node-v24.msi /qn /norestart'
stdin, stdout, stderr = c.exec_command(cmd)
stdout.read()
import time
time.sleep(5)
# 验证
stdin, stdout, stderr = c.exec_command('node --version')
print('New node:', stdout.read().decode('utf-8').strip())
```
**输出:**
```
New node: v24.15.0
```
### 2.3 安装 OpenClaw
```python
# 安装最新版
stdin, stdout, stderr = c.exec_command('npm install -g openclaw@latest 2>&1')
out = stdout.read().decode('utf-8', 'replace')
print(out[-1000:]) # 打印最后 1000 字符
# 验证版本
stdin, stdout, stderr = c.exec_command('openclaw --version 2>&1')
print(stdout.read().decode('utf-8').strip())
```
**输出:**
```
OpenClaw 2026.7.1-2 (0790d9f)
```
### 2.4 写入配置文件
生成 `openclaw.json`:
```python
import json
config = {
"agents": {
"defaults": {
"workspace": "C:\\Users\\Administrator\\.openclaw\\workspace",
"models": {
"qwen/qwen3.7-plus": {},
"qwen/qwen3.6-plus": {}
},
"model": {
"primary": "qwen/qwen3.6-plus",
"fallbacks": ["qwen/qwen3.7-plus"]
}
}
},
"gateway": {
"mode": "local",
"auth": {
"mode": "token",
"token": "your-s…here"
},
"port": 18789,
"bind": "loopback",
"controlUi": {
"allowInsecureAuth": True
}
},
"models": {
"mode": "merge",
"providers": {
"qwen": {
"baseUrl": "https://coding.dashscope.aliyuncs.com/v1",
"api": "openai-completions",
"apiKey": "***",
"models": [
{
"id": "qwen3.7-plus",
"name": "qwen3.7-plus",
"reasoning": True,
"input": ["text", "image"],
"cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0},
"contextWindow": 1000000,
"maxTokens": 65536
},
{
"id": "qwen3.6-plus",
"name": "qwen3.6-plus",
"reasoning": True,
"input": ["text", "image"],
"cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0},
"contextWindow": 1000000,
"maxTokens": 65536
}
]
}
}
},
"auth": {
"profiles": {
"qwen:default": {
"provider": "qwen",
"mode": "api_key"
}
}
}
}
config_json = json.dumps(config, indent=2, ensure_ascii=False)
# 通过 SFTP 写入文件
sftp = c.open_sftp()
with sftp.file('C:\\Users\\Administrator\\.openclaw\\openclaw.json', 'w') as f:
f.write(config_json)
sftp.close()
print('Config written successfully')
```
### 2.5 启动 Gateway
**方案一:使用 wmic(推荐)**
```python
# 使用 wmic 创建后台进程
cmd = (
'wmic process call create '
'"\\"C:\\Program Files\\nodejs\\node.exe\\" '
'\\"C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\openclaw\\openclaw.mjs\\" '
'gateway run"'
)
stdin, stdout, stderr = c.exec_command(cmd)
out = stdout.read().decode('utf-8', 'replace')
print(out)
import time
time.sleep(6)
# 验证端口
stdin, stdout, stderr = c.exec_command('netstat -an | findstr "18789.*LISTEN"')
out = stdout.read().decode('utf-8', 'replace')
print('Port 18789:', out.strip() if out.strip() else 'NOT listening')
```
**方案二:使用计划任务(开机自启)**
```python
cmd = (
'schtasks /create /tn "OpenClaw Gateway" '
'/tr "\\"C:\\Program Files\\nodejs\\node.exe\\" '
'\\"C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\openclaw\\openclaw.mjs\\" gateway run" '
'/sc onlogon /rl highest /f'
)
stdin, stdout, stderr = c.exec_command(cmd)
out = stdout.read().decode('utf-8', 'replace')
print(out)
```
### 2.6 验证安装
```python
stdin, stdout, stderr = c.exec_command('openclaw status 2>&1')
out = stdout.read().decode('utf-8', 'replace')
print(out)
```
**期望输出:**
```
OpenClaw status
Overview
+----------------------+-----------------------------------------------------------------------------------------------+
| Item | Value |
+----------------------+-----------------------------------------------------------------------------------------------+
| OS | windows 10.0.19045 (x64) · node 24.15.0 |
| Dashboard | http://127.0.0.1:18789/ |
| Gateway | local · ws://127.0.0.1:18789 (local loopback) · reachable 123ms · auth token |
| Gateway service | Scheduled Task not installed |
| Agents | 1 · 1 bootstrap file present · sessions 1 · default main active 39m ago |
| Sessions | 1 active · default qwen3.6-plus (1.0m ctx) |
+----------------------+-----------------------------------------------------------------------------------------------+
```
---
## 三、完整自动化脚本
将上述步骤整合为一个完整的 Python 脚本:
```python
import paramiko
import json
import time
import sys
sys.stdout.reconfigure(encoding='utf-8')
# 配置参数
TARGET_IP = '192.168.3.93'
USERNAME = 'Administrator'
PASSWORD = '***'
API_KEY = '***'
GATEWAY_TOKEN = '***'
def main():
print('=== OpenClaw 远程自动安装脚本 ===\n')
# 1. 建立 SSH 连接
print('[1/6] 建立 SSH 连接...')
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(TARGET_IP, username=USERNAME, password=*** timeout=10)
print('✓ 连接成功\n')
# 2. 检查/安装 Node.js
print('[2/6] 检查 Node.js...')
stdin, stdout, stderr = c.exec_command('node --version 2>&1')
node_ver = stdout.read().decode('utf-8').strip()
print(f'当前版本: {node_ver}')
if 'v22.16' in node_ver or 'not found' in node_ver.lower():
print('需要升级 Node.js...')
# 下载并安装 Node.js 24
cmd = 'powershell -Command "Invoke-WebRequest -Uri https://nodejs.org/dist/v24.15.0/node-v24.15.0-x64.msi -OutFile C:\\Users\\Administrator\\node-v24.msi"'
c.exec_command(cmd)[1].read()
c.exec_command('msiexec /i C:\\Users\\Administrator\\node-v24.msi /qn /norestart')[1].read()
time.sleep(5)
stdin, stdout, stderr = c.exec_command('node --version')
print(f'✓ 新版本: {stdout.read().decode("utf-8").strip()}\n')
else:
print('✓ Node.js 版本符合要求\n')
# 3. 安装 OpenClaw
print('[3/6] 安装 OpenClaw...')
stdin, stdout, stderr = c.exec_command('npm install -g openclaw@latest 2>&1')
stdout.read() # 等待完成
stdin, stdout, stderr = c.exec_command('openclaw --version 2>&1')
print(f'✓ {stdout.read().decode("utf-8").strip()}\n')
# 4. 写入配置文件
print('[4/6] 写入配置文件...')
config = {
"agents": {
"defaults": {
"workspace": "C:\\Users\\Administrator\\.openclaw\\workspace",
"model": {
"primary": "qwen/qwen3.6-plus",
"fallbacks": ["qwen/qwen3.7-plus"]
}
}
},
"gateway": {
"mode": "local",
"auth": {"mode": "token", "token": GATEWAY_TOKEN},
"port": 18789,
"bind": "loopback"
},
"models": {
"providers": {
"qwen": {
"baseUrl": "https://coding.dashscope.aliyuncs.com/v1",
"api": "openai-completions",
"apiKey": API_KEY,
"models": [
{"id": "qwen3.7-plus", "name": "qwen3.7-plus", "reasoning": True,
"input": ["text", "image"], "contextWindow": 1000000, "maxTokens": 65536},
{"id": "qwen3.6-plus", "name": "qwen3.6-plus", "reasoning": True,
"input": ["text", "image"], "contextWindow": 1000000, "maxTokens": 65536}
]
}
}
},
"auth": {
"profiles": {"qwen:default": {"provider": "qwen", "mode": "api_key"}}
}
}
sftp = c.open_sftp()
with sftp.file('C:\\Users\\Administrator\\.openclaw\\openclaw.json', 'w') as f:
f.write(json.dumps(config, indent=2))
sftp.close()
print('✓ 配置完成\n')
# 5. 启动 Gateway
print('[5/6] 启动 Gateway...')
cmd = (
'wmic process call create '
'"\\"C:\\Program Files\\nodejs\\node.exe\\" '
'\\"C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\openclaw\\openclaw.mjs\\" '
'gateway run"'
)
c.exec_command(cmd)
time.sleep(6)
stdin, stdout, stderr = c.exec_command('netstat -an | findstr "18789.*LISTEN"')
port_status = stdout.read().decode('utf-8').strip()
if port_status:
print('✓ Gateway 已启动,端口 18789 监听中\n')
else:
print('✗ Gateway 启动失败\n')
# 6. 设置开机自启
print('[6/6] 设置开机自启...')
cmd = (
'schtasks /create /tn "OpenClaw Gateway" '
'/tr "\\"C:\\Program Files\\nodejs\\node.exe\\" '
'\\"C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\openclaw\\openclaw.mjs\\" gateway run" '
'/sc onlogon /rl highest /f'
)
stdin, stdout, stderr = c.exec_command(cmd)
out = stdout.read().decode('utf-8', 'replace')
if '成功' in out or 'success' in out.lower():
print('✓ 计划任务创建成功\n')
else:
print('✗ 计划任务创建失败\n')
# 完成
print('=== 安装完成 ===')
print(f'Dashboard: http://{TARGET_IP}:18789/')
print('注意:Dashboard 只能在本机访问(bind: loopback)')
c.close()
if __name__ == '__main__':
main()
```
---
## 四、常见问题
### Q1: SSH 连接超时
**解决方案:**
1. 确认目标机器已开机并联网
2. 检查 IP 地址是否正确(`ipconfig`)
3. 确认 OpenSSH Server 服务已启动(`Get-Service sshd`)
4. 检查防火墙是否放行 22 端口
### Q2: Node.js 版本不兼容
**错误信息:**
```
openclaw: Node.js >=22.22.3 <23, >=24.15.0 <25, or >=25.9.0 is required
```
**解决方案:**
下载并安装最新版 Node.js LTS:
```
https://nodejs.org/dist/v24.15.0/node-v24.15.0-x64.msi
```
### Q3: Gateway 启动失败
**排查步骤:**
1. 检查端口是否被占用:`netstat -an | findstr 18789`
2. 查看日志:`type C:\Users\Administrator\.openclaw\gw.log`
3. 尝试前台运行看错误:`openclaw gateway run`
**解决方案:**
使用 `wmic` 创建独立进程,而不是 `start /b`。
### Q4: 配置文件格式错误
**解决方案:**
确保 JSON 格式正确,可以使用在线 JSON 校验工具:
```
https://jsonlint.com/
```
---
## 五、安全建议
1. **修改默认密码**:安装完成后立即修改 SSH 密码
2. **使用强 Token**:Gateway token 建议使用随机生成的长字符串
3. **API Key 保护**:不要将 API Key 提交到 Git 仓库
4. **防火墙配置**:如果不需要远程访问,保持 `bind: loopback`
5. **定期更新**:运行 `npm update -g openclaw` 保持最新版本
---
## 六、总结
通过本文的教程,我们实现了:
✅ 通过 SSH 远程连接 Windows 电脑
✅ 自动安装 Node.js 和 OpenClaw
✅ 自动写入配置文件
✅ 启动 Gateway 并设置开机自启
整个过程无需在目标机器上手动操作,适合批量部署或远程维护场景。
**相关资源:**
- OpenClaw GitHub:https://github.com/openclaw/openclaw
- OpenClaw 文档:https://docs.openclaw.ai
- Node.js 下载:https://nodejs.org/
- openshh 下载:https://freeshelf.cn/
---
*如果本文对你有帮助,欢迎点赞、收藏、关注。有问题欢迎评论区讨论。*更多推荐

所有评论(0)