实验环境 API

在线Linux实验环境管理和操作相关的API接口文档

API 概述

实验环境API提供虚拟Linux环境的创建、管理、操作等功能。 用户可以通过API创建独立的实验环境进行学习和实践。

环境管理

创建、启动和停止实验环境

终端操作

WebSocket终端连接和命令执行

资源监控

系统资源使用情况监控

环境管理

POST /api/playground/environments

创建新的实验环境

POST https://api.linuxcloudlab.com/playground/environments
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
Content-Type: application/json
{
"template": "ubuntu-20.04",
"name": "我的Linux实验",
"description": "学习基本命令",
"resources": {
"cpu": 1,
"memory": "1GB",
"storage": "10GB"
}
}
响应示例
{
"success": true,
"data": {
"environmentId": "env_123456",
"status": "creating",
"accessUrl": "https://terminal.linuxcloudlab.com/env_123456",
"createdAt": "2025-12-15T10:30:00Z",
"expiresAt": "2025-12-15T14:30:00Z"
}
}

GET /api/playground/environments

获取用户的实验环境列表

GET https://api.linuxcloudlab.com/playground/environments
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

PUT /api/playground/environments/{envId}/start

启动实验环境

PUT https://api.linuxcloudlab.com/playground/environments/env_123456/start
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

PUT /api/playground/environments/{envId}/stop

停止实验环境

PUT https://api.linuxcloudlab.com/playground/environments/env_123456/stop
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

终端连接

WebSocket /api/playground/environments/{envId}/terminal

建立WebSocket终端连接

wss://api.linuxcloudlab.com/playground/environments/env_123456/terminal
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
消息格式
// 发送命令
{
"type": "command",
"data": "ls -la"
}
// 接收输出
{
"type": "output",
"data": "total 24\\ndrwxr-xr-x 3 user user 4096 Dec 15 10:30 ."
}

POST /api/playground/environments/{envId}/execute

执行单个命令(HTTP方式)

POST https://api.linuxcloudlab.com/playground/environments/env_123456/execute
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
Content-Type: application/json
{
"command": "pwd",
"timeout": 30
}

文件管理

GET /api/playground/environments/{envId}/files

获取文件列表

GET https://api.linuxcloudlab.com/playground/environments/env_123456/files?path=/home/user
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
响应示例
{
"success": true,
"data": {
"path": "/home/user",
"files": [
{
"name": "script.sh",
"type": "file",
"size": 1024,
"permissions": "rwxr-xr-x",
"modified": "2025-12-15T10:30:00Z"
}
]
}
}

POST /api/playground/environments/{envId}/files

上传文件

POST https://api.linuxcloudlab.com/playground/environments/env_123456/files
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
Content-Type: multipart/form-data
FormData:
file: [File object]
path: "/home/user/uploads/"

GET /api/playground/environments/{envId}/files/download

下载文件

GET https://api.linuxcloudlab.com/playground/environments/env_123456/files/download?path=/home/user/script.sh
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

资源监控

GET /api/playground/environments/{envId}/stats

获取环境资源使用统计

GET https://api.linuxcloudlab.com/playground/environments/env_123456/stats
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
响应示例
{
"success": true,
"data": {
"cpu": {
"usage": 25.5,
"cores": 1
},
"memory": {
"used": 512,
"total": 1024,
"usage": 50.0
},
"storage": {
"used": 2048,
"total": 10240,
"usage": 20.0
},
"network": {
"bytesIn": 1024000,
"bytesOut": 512000
}
}
}

GET /api/playground/environments/{envId}/logs

获取环境日志

GET https://api.linuxcloudlab.com/playground/environments/env_123456/logs?lines=100
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

环境配置

可用模板

  • • ubuntu-20.04 - Ubuntu 20.04 LTS
  • • ubuntu-22.04 - Ubuntu 22.04 LTS
  • • centos-8 - CentOS 8
  • • debian-11 - Debian 11
  • • alpine-3.16 - Alpine Linux 3.16

资源限制

免费用户
  • CPU: 1核心
  • 内存: 1GB
  • 存储: 10GB
  • 时长: 4小时
高级用户
  • CPU: 最多4核心
  • 内存: 最多8GB
  • 存储: 最多100GB
  • 时长: 24小时

相关API

查看其他相关的API文档