vim harbor-images-list.sh
#!/bin/bash
#镜像清单文件,将获取到的镜像信息存到该文件中
File=harbor-images-`date '+%Y-%m-%d'`.txt
## 定义Harbor连接地址,这里需要改为你们自己的Harbor地址
Address=http://10.10.10.10:8080
## 定义连接Harbor的用户名和密码(因为是获取全部的镜像,只有admin用户才有该权限)
Hamin=admin:Harbor12345
## 获取Harbor中有哪些项目(Project)
Project_List=$(curl -u "$Hamin" -X GET $Address/api/projects -H "Content-Type: application/json" | grep name | awk '/"name": /' | awk -F '"' '{print $4}')
for Project in $Project_List;do
# 循环获取每个项目下所有的镜像
Image_Names=$(curl -u "$Hamin" -X GET $Address/api/search?q=$Project -H "Content-Type: application/json" | grep "repository_name" | awk -F "\"" '{print $4}')
for Image in $Image_Names;do
# 循环获取每个镜像所有的标签(版本)
Image_Tags=$(curl -u "$Hamin" -X GET $Address/api/repositories/$Image/tags -H "Content-Type: application/json" | awk '/"name": /' | awk -F '"' '{print $4}')
for Tag in $Image_Tags;do
# 将获取到的镜像完整路径存档到镜像清单文件
echo "$Address/$Image:$Tag" | grep -v Base | grep -v Image | grep -v CentOS >> $File
done
done
done
评论区