侧边栏壁纸
  • 累计撰写 16 篇文章
  • 累计收到 4 条评论

Redfish 用户配置

2025-8-5 / 0 评论 / 6591 阅读

Redfish 用户配置迁移指南

一、环境准备

  1. 安装工具

    • curl:命令行 HTTP 客户端
    • jq:JSON 解析工具(用于命令行)
    • Python 3(可选,用于脚本操作)
  2. 目标设备要求

    • 已启用 Redfish 服务
    • 管理员账户(用于认证)
    • 确保网络可达

二、导出用户配置

1. 导出所有用户列表

curl -k -u 管理员用户名:密码 https://源设备IP/redfish/v1/AccountService/Accounts/ | python3 -c "import sys, json; print(json.dumps(json.load(sys.stdin), indent=2))" > users_list.json

2. 导出单个用户详细配置

curl -k -u 管理员用户名:密码 https://源设备IP/redfish/v1/AccountService/Accounts/1 | python3 -c "import sys, json; print(json.dumps(json.load(sys.stdin), indent=2))" > user_1.json

3. 批量导出所有用户(Python 脚本)

import requests
import json
import os
from requests.packages.urllib3.exceptions import InsecureRequestWarning

# 禁用SSL证书验证警告
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

# 配置信息
source_ip = "源设备IP"
admin_user = "管理员用户名"
admin_pass = "管理员密码"
output_dir = "redfish_users_config"  # 保存配置的文件夹

# 创建输出目录
os.makedirs(output_dir, exist_ok=True)

try:
    # 1. 获取用户列表
    accounts_url = f"https://{source_ip}/redfish/v1/AccountService/Accounts/"
    response = requests.get(
        accounts_url,
        auth=(admin_user, admin_pass),
        verify=False
    )
    response.raise_for_status()
    users_data = response.json()

    # 保存用户列表
    with open(f"{output_dir}/users_list.json", "w") as f:
        json.dump(users_data, f, indent=2)

    # 2. 逐个导出用户详细配置
    for member in users_data.get("Members", []):
        user_url = member["@odata.id"]
        # 处理相对路径,拼接完整URL
        if user_url.startswith("/"):
            user_full_url = f"https://{source_ip}{user_url}"
        else:
            user_full_url = f"https://{source_ip}/redfish/v1/AccountService/Accounts/{user_url}"

        # 获取用户详情
        user_response = requests.get(
            user_full_url,
            auth=(admin_user, admin_pass),
            verify=False
        )
        user_response.raise_for_status()
        user_data = user_response.json()

        # 提取用户ID作为文件名
        user_id = user_url.split("/")[-1]
        with open(f"{output_dir}/user_{user_id}.json", "w") as f:
            json.dump(user_data, f, indent=2)

        print(f"已导出用户 {user_data.get('UserName')}(ID: {user_id})")

    print(f"\n所有用户配置已保存到 {output_dir} 文件夹")

except Exception as e:
    print(f"导出失败:{str(e)}")

三、导入用户配置

1. 手动导入单个用户(curl 命令)

curl -k -u 管理员用户名:密码 \
  -X POST \
  -H "Content-Type: application/json" \
  -d '{
    "UserName": "zabbix",
    "Password": "SecurePass123!",
    "RoleId": "Administrator",
    "Enabled": true,
    "LoginInterface": ["SNMP", "IPMI", "SSH", "SFTP", "Local", "Redfish"]
  }' \
  https://目标设备IP/redfish/v1/AccountService/Accounts/

2. 批量导入所有用户(Shell 脚本)

#!/bin/bash

# 目标设备信息
TARGET_IP="目标设备IP"
ADMIN_USER="管理员用户名"
ADMIN_PASS="管理员密码"
CONFIG_DIR="redfish_users_config"  # 存放用户配置的文件夹

# 循环处理每个用户配置文件
for file in "$CONFIG_DIR"/user_*.json; do
  # 从配置文件中提取关键信息
  username=$(jq -r '.UserName' "$file")
  role_id=$(jq -r '.RoleId' "$file")
  enabled=$(jq -r '.Enabled' "$file")
  login_interface=$(jq -r '.LoginInterface | @json' "$file")  # 提取登录接口列表

  # 从密码文件中获取对应密码
  password=$(jq -r --arg user "$username" '.[$user]' passwords.json)

  # 跳过未配置密码的用户
  if [ "$password" == "null" ]; then
    echo "跳过用户 $username(未配置密码)"
    continue
  fi

  # 发送创建用户的请求
  echo "正在创建用户 $username..."
  curl -k -u "$ADMIN_USER:$ADMIN_PASS" \
    -X POST \
    -H "Content-Type: application/json" \
    -d '{
      "UserName": "'"$username"'",
      "Password": "'"$password"'",
      "RoleId": "'"$role_id"'",
      "Enabled": '"$enabled"',
      "LoginInterface": '"$login_interface"'
    }' \
    "https://$TARGET_IP/redfish/v1/AccountService/Accounts/"

  echo -e "\n-------------------------"
done

3. 密码映射文件(passwords.json)

{
  "admin": "Admin@123456",
  "operator": "Oper@654321",
  "viewer": "View@987654"
}

四、高级配置(包含 LoginInterface)

1. 导出时保留 LoginInterface

确保导出的用户配置文件包含 LoginInterface 字段(如示例中的 zabbix 用户):

{
  "UserName": "zabbix",
  "RoleId": "Administrator",
  "Enabled": true,
  "LoginInterface": [
    "SNMP",
    "IPMI",
    "SSH",
    "SFTP",
    "Local",
    "Redfish"
  ]
}

2. 导入时自动配置 LoginInterface

通过上述批量导入脚本,自动从配置文件中提取 LoginInterface 并导入。

python3 -c "import sys, json; print(json.dumps(json.load(sys.stdin), indent=2))"
也可以用 python3 -m json.tool

五、注意事项

  1. 密码策略

    • 目标设备可能有严格的密码复杂度要求(如长度、字符类型)
    • 建议使用符合策略的密码,或在导入前检查目标设备的密码规则
  2. 角色一致性

    • 确保目标设备存在与源设备相同的 RoleId(如 Administrator
    • 可通过 /redfish/v1/AccountService/Roles/ 路径检查角色列表
  3. 厂商差异

    • 不同厂商(如 Dell、HPE、华为)的 Redfish 实现可能略有差异
    • 建议参考目标设备的官方文档,确认支持的字段和格式
  4. 安全性

    • 避免在脚本中明文存储密码,建议使用环境变量或加密存储
    • 生产环境中启用 SSL 证书验证(verify=True
  5. 错误处理

    • 导入失败时,检查返回的状态码和错误信息
    • 常见错误:角色不存在、密码不符合策略、权限不足等

评论一下?

OωO
取消