1Panel命令行:CLI工具与API调用
【免费下载链接】1Panel 新一代的 Linux 服务器运维管理面板 项目地址: https://gitcode.com/feizhiyun/1Panel
引言
还在为Linux服务器管理而烦恼?每次都需要登录Web界面进行简单操作?1Panel作为新一代的Linux服务器运维管理面板,不仅提供了强大的Web图形界面,还内置了完整的命令行工具(CLI)和丰富的API接口,让您能够通过脚本和命令行实现自动化运维管理。
通过本文,您将掌握:
- 1Panel CLI工具的核心命令和使用方法
- API接口的认证机制和调用方式
- 实际场景中的自动化脚本编写技巧
- 批量管理和监控服务器的实战方案
1Panel CLI工具详解
安装与基本配置
1Panel CLI工具随面板安装自动部署,位于 /usr/local/bin/1panel。要使用CLI功能,首先需要确保已安装1Panel:
# 检查1Panel安装状态
which 1panel
# 查看版本信息
sudo 1panel version
核心命令功能
1Panel CLI基于Cobra框架构建,提供了丰富的子命令:
版本查询命令
# 查看当前系统版本
sudo 1panel version
# 输出示例:
SystemVersion: v1.8.0
SystemMode: professional
用户信息管理
# 获取用户信息(需要root权限)
sudo 1panel user-info
系统维护命令
# 系统恢复操作
sudo 1panel restore --file /path/to/backup.tar.gz
# 重置管理员密码
sudo 1panel reset --username admin --password newpassword
# 配置监听IP地址
sudo 1panel listen-ip --ip 192.168.1.100 --port 4004
高级用法:批量脚本示例
#!/bin/bash
# 批量服务器健康检查脚本
SERVER_LIST=("server1" "server2" "server3")
PORT=4004
for server in "${SERVER_LIST[@]}"; do
echo "检查服务器: $server"
ssh $server "sudo 1panel version"
# 检查服务状态
if curl -s "http://$server:$PORT/api/v2/dashboard/current" > /dev/null; then
echo "$server: 服务正常运行"
else
echo "$server: 服务异常"
fi
done
API接口调用指南
API认证机制
1Panel API采用双重认证机制:
- API Key认证:需要在面板设置中生成API密钥
- 时间戳签名:防止重放攻击
认证头格式
1Panel-Token: md5('1panel' + API-Key + UnixTimestamp)
Timestamp: 1704067200
生成API密钥
首先需要在Web界面生成API密钥:
- 登录1Panel管理界面
- 进入"设置" → "API配置"
- 点击"生成密钥"获取API Key
- 配置IP白名单(可选)
核心API端点
系统状态查询
# 获取仪表板当前状态
curl -X GET "http://localhost:4004/api/v2/dashboard/current" \
-H "1Panel-Token: $(echo -n "1panel${API_KEY}${TIMESTAMP}" | md5sum | cut -d' ' -f1)" \
-H "Timestamp: ${TIMESTAMP}"
容器管理API
# 获取容器列表
curl -X GET "http://localhost:4004/api/v2/containers" \
-H "1Panel-Token: ${TOKEN}" \
-H "Timestamp: ${TIMESTAMP}"
# 启动指定容器
curl -X POST "http://localhost:4004/api/v2/containers/nginx/start" \
-H "1Panel-Token: ${TOKEN}" \
-H "Timestamp: ${TIMESTAMP}" \
-H "Content-Type: application/json"
网站管理API
# 创建新网站
curl -X POST "http://localhost:4004/api/v2/websites" \
-H "1Panel-Token: ${TOKEN}" \
-H "Timestamp: ${TIMESTAMP}" \
-H "Content-Type: application/json" \
-d '{
"primaryDomain": "example.com",
"type": "php",
"phpVersion": "8.2",
"remark": "生产环境网站"
}'
API调用工具函数
为了方便API调用,可以创建专门的工具脚本:
#!/bin/bash
# 1panel_api.sh - API调用工具函数
API_BASE="http://localhost:4004/api/v2"
API_KEY="your_api_key_here"
generate_token() {
local timestamp=$(date +%s)
local raw_token="1panel${API_KEY}${timestamp}"
local token=$(echo -n "$raw_token" | md5sum | cut -d' ' -f1)
echo "$token $timestamp"
}
call_api() {
local endpoint=$1
local method=${2:-GET}
local data=${3:-}
read token timestamp <<< $(generate_token)
local curl_cmd="curl -s -X $method"
curl_cmd+=" -H '1Panel-Token: $token'"
curl_cmd+=" -H 'Timestamp: $timestamp'"
curl_cmd+=" -H 'Content-Type: application/json'"
if [ -n "$data" ]; then
curl_cmd+=" -d '$data'"
fi
curl_cmd+=" ${API_BASE}${endpoint}"
eval $curl_cmd
}
# 使用示例
get_dashboard() {
call_api "/dashboard/current"
}
list_containers() {
call_api "/containers"
}
create_website() {
local data='{"primaryDomain":"test.com","type":"php","phpVersion":"8.2"}'
call_api "/websites" "POST" "$data"
}
实战场景应用
场景一:自动化备份与监控
#!/bin/bash
# 自动化备份监控脚本
LOG_FILE="/var/log/1panel_backup.log"
API_KEY="your_api_key"
# 执行数据库备份
backup_result=$(call_api "/backups/database" "POST" '{"type":"full"}')
echo "$(date): 数据库备份结果: $backup_result" >> $LOG_FILE
# 检查备份状态
backup_status=$(call_api "/backups/status")
if echo "$backup_status" | grep -q "success"; then
echo "$(date): 备份成功" >> $LOG_FILE
else
echo "$(date): 备份失败,发送告警" >> $LOG_FILE
# 发送告警通知
call_api "/alerts" "POST" '{"type":"backup_failed","message":"数据库备份失败"}'
fi
场景二:多服务器批量管理
#!/bin/bash
# 多服务器配置同步脚本
SERVERS=("192.168.1.101:4004" "192.168.1.102:4004" "192.168.1.103:4004")
CONFIG_FILE="/path/to/nginx/config.conf"
for server in "${SERVERS[@]}"; do
echo "同步配置到服务器: $server"
# 上传配置文件
upload_result=$(curl -X POST "http://$server/api/v2/files/upload" \
-H "1Panel-Token: ${TOKEN}" \
-H "Timestamp: ${TIMESTAMP}" \
-F "file=@$CONFIG_FILE" \
-F "path=/etc/nginx/conf.d/")
# 重载Nginx配置
if [ "$upload_result" = "success" ]; then
curl -X POST "http://$server/api/v2/nginx/reload" \
-H "1Panel-Token: ${TOKEN}" \
-H "Timestamp: ${TIMESTAMP}"
echo "服务器 $server 配置同步完成"
else
echo "服务器 $server 配置同步失败"
fi
done
场景三:资源监控与告警
#!/bin/bash
# 资源监控脚本
THRESHOLD_CPU=80
THRESHOLD_MEM=85
THRESHOLD_DISK=90
# 获取系统状态
system_status=$(call_api "/dashboard/current")
cpu_usage=$(echo "$system_status" | jq '.cpuUsage')
mem_usage=$(echo "$system_status" | jq '.memoryUsage')
disk_usage=$(echo "$system_status" | jq '.diskUsage')
# 检查阈值并发送告警
check_threshold() {
local metric=$1
local value=$2
local threshold=$3
local alert_type=$4
if (( $(echo "$value > $threshold" | bc -l) )); then
call_api "/alerts" "POST" \
"{\"type\":\"$alert_type\",\"message\":\"$metric 使用率超过阈值: $value%\"}"
fi
}
check_threshold "CPU" "$cpu_usage" "$THRESHOLD_CPU" "high_cpu"
check_threshold "内存" "$mem_usage" "$THRESHOLD_MEM" "high_memory"
check_threshold "磁盘" "$disk_usage" "$THRESHOLD_DISK" "high_disk"
安全最佳实践
API安全配置
# 定期轮换API密钥
0 0 1 * * /usr/local/bin/1panel api-key-rotate
# 配置IP白名单
echo "192.168.1.0/24" > /etc/1panel/api-whitelist.conf
# 启用API访问日志
sed -i 's/API_LOGGING=false/API_LOGGING=true/' /etc/1panel/config
权限控制
# 创建专用API用户
useradd -r -s /bin/false api-user
# 设置最小权限原则
chown -R api-user:api-user /etc/1panel/api-keys/
chmod 600 /etc/1panel/api-keys/*
故障排除与调试
常见问题解决
# 检查API服务状态
systemctl status 1panel-api
# 查看API日志
tail -f /var/log/1panel/api.log
# 测试API连通性
curl -v http://localhost:4004/api/v2/health
# 重置API配置
1panel api-config --reset
调试模式
# 启用详细日志
【免费下载链接】1Panel 新一代的 Linux 服务器运维管理面板 项目地址: https://gitcode.com/feizhiyun/1Panel
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



