# 示例命令

本指南提供了 Zenlayer Cloud CLI 的常见操作示例命令，帮助您快速上手使用 CLI 管理 Zenlayer Cloud 资源。

## 配置相关示例

### 1. 配置 CLI

```bash
# 交互式配置
zeno configure

# 查看当前配置
zeno configure list

# 获取特定配置值
zeno configure get output

# 设置特定配置值
zeno configure set output table
```

### 2. 使用不同配置文件

```bash
# 使用 prod 配置文件
zeno --profile prod zec describe-instances

# 使用环境变量指定配置文件
export ZENLAYER_PROFILE=dev
zeno zec describe-instances
```

## 云服务器 (ZEC) 示例

### 1. 创建云服务器实例

```bash
# 创建云服务器实例
zeno zec create-zec-instances \
  --instance-type zec.s1.small \
  --count 1 \
  --region-id asia-east-1 \
  --image-id img-123456 \
  --name my-instance \
  --password MyPassword123!

# 创建多个云服务器实例
zeno zec create-zec-instances \
  --instance-type zec.s2.medium \
  --count 3 \
  --region-id asia-east-1 \
  --image-id img-123456 \
  --name batch-instance
```

### 2. 管理云服务器实例

```bash
# 查看云服务器实例列表
zeno zec describe-instances

# 查看特定云服务器实例
zeno zec describe-instances --instance-ids 123456

# 启动云服务器实例
zeno zec start-instances --instance-ids 123456

# 停止云服务器实例
zeno zec stop-instances --instance-ids 123456

# 重启云服务器实例
zeno zec reboot-instances --instance-ids i-123456

# 释放云服务器实例
zeno zec release-instances --instance-ids i-123456
```

### 3. 管理云服务器实例属性

```bash
# 修改云服务器实例名称
zeno zec modify-instance-attribute \
  --instance-id 123456 \
  --name new-instance-name

# 修改云服务器实例密码
zeno zec modify-instance-attribute \
  --instance-id 123456 \
  --password NewPassword123!
```

## 高级使用示例

### 1. 使用输出格式和查询

```bash
# 使用表格格式输出
zeno zec describe-instances --output table

# 使用 JSON 格式输出
zeno zec describe-instances --output json

# 提取特定字段
zeno zec describe-instances --query "dataSet[*].instanceId"

# 过滤数据
zeno zec describe-instances --query "dataSet[?status=='running']"

# 复杂查询
zeno zec describe-instances --query "dataSet[?zoneId=='ap-hongkong-1' && status=='running'].{ID: instanceId, Name: name}"
```

### 2. 结合 shell 命令

```bash
# 提取所有实例 ID 并存储到变量
instance_ids=$(zeno zec describe-instances --query "dataSet[*].instanceId" --output json | jq -r ".[]")

# 遍历实例 ID 并查看详细信息
for instance_id in $instance_ids; do
  echo "Checking instance: $instance_id"
  zeno zec describe-instances --instance-ids $instance_id --output table
done

# 保存输出到文件
zeno zec describe-instances --output json > instances.json
zeno traffic describe-bandwidth-clusters --output table > bandwidth-clusters.txt
```

### 3. 使用环境变量

```bash
# 设置环境变量
export ZENLAYER_PROFILE=prod
export ZENLAYER_OUTPUT=table

# 执行命令
zeno zec describe-instances

# 临时覆盖环境变量
ZENLAYER_OUTPUT=json zeno traffic describe-bandwidth-clusters
```

## 常见使用场景

### 场景 1：自动化部署云服务器实例

```bash
#!/bin/bash

# 设置环境变量
export ZENLAYER_PROFILE=prod

# 创建云服务器实例
instance_id=$(zeno zec create-zec-instances \
  --instance-type zec.s1.small \
  --count 1 \
  --region-id asia-east-1 \
  --image-id img-123456 \
  --name auto-deploy-instance \
  --password MyPassword123! \
  --output json \
  --query "instanceIds[0]")

# 等待实例启动
echo "Waiting for instance to start..."
sleep 60

# 查看实例状态
zeno zec describe-instances --instance-ids $instance_id --output table

echo "Instance deployed successfully: $instance_id"
```

### 场景 2：监控带宽使用情况

```bash
#!/bin/bash

# 设置环境变量
export ZENLAYER_PROFILE=prod

# 获取所有带宽集群
clusters=$(zeno traffic describe-bandwidth-clusters --query "dataSet[*].clusterId" --output json | jq -r ".[]")

# 遍历集群并获取使用情况
for cluster in $clusters; do
  echo "Checking bandwidth cluster: $cluster"
  zeno traffic describe-bandwidth-cluster-usage \
    --cluster-id $cluster \
    --start-time $(date -u -v-7d +"%Y-%m-%dT%H:%M:%SZ") \
    --end-time $(date -u +"%Y-%m-%dT%H:%M:%SZ") \
    --output table
done
```

### 场景 3：批量管理资源

```bash
#!/bin/bash

# 设置环境变量
export ZENLAYER_PROFILE=prod

# 获取所有已停止的云服务器实例
stopped_instances=$(zeno zec describe-instances --query "dataSet[?status=='stopped'].instanceId" --output json | jq -r ".[]")

# 遍历并释放已停止的云服务器实例
for instance in $stopped_instances; do
  echo "Releasing stopped instance: $instance"
  zeno zec release-instances --instance-ids $instance
done
```

## 最佳实践

* **使用配置文件**：为不同环境创建配置文件，方便切换
* **使用输出格式**：根据需要选择合适的输出格式
* **使用查询过滤**：使用 `--query` 选项过滤输出，只获取需要的信息
* **结合 shell 命令**：将 CLI 命令与 shell 命令结合，实现更复杂的自动化
* **编写脚本**：将常用操作编写为脚本，提高工作效率
* **使用环境变量**：在 CI/CD 环境中使用环境变量设置凭证

## 相关文档

* [命令结构](https://docs.console.zenlayer.com/zenlayer-cli/cn/usage/command-structure) - 了解 CLI 命令的基本结构
* [参数类型](https://docs.console.zenlayer.com/zenlayer-cli/cn/usage/parameter-types) - 了解不同类型参数的使用方法
* [输出格式](https://docs.console.zenlayer.com/zenlayer-cli/cn/usage/output) - 了解不同输出格式的使用方法


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.console.zenlayer.com/zenlayer-cli/cn/usage/examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
