使用Curl命令发送POST请求批量添加用户脚本

#!/bin/bash
Date=$(date +%Y%m%d)
Log_File="result-${Date}.log"
Log_CMD="tee -a $Log_File"

custId="324c1737856149d19e027ad45e869666"
Authorization="Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dpblR5cGUiOiJiYXNpYyIsInVzZXJfbmFtZSI6Indhbmdob25nd2VpIiwidXNlcl9zb3VyY2UiOiJURU5BTlQiLCJwYWFzVG9rZW4iOiIwMWZjNjMyMzlkZTA3OWRhZjI5NjFmNjIyMjA5MzgxYiIsImNhc0NsaWVudEFkZHIiOiIxNzIuMTguNDIuNzYiLCJ1c2VyQWdlbnQiOiJNb3ppbGxhLzUuMCAoV2luZG93cyBOVCAxMC4wOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvOTguMC40NzU4LjEwMiBTYWZhcmkvNTM3LjM2IiwiZGF0YV9zb3VyY2UiOiJURU5BTlQiLCJjbGllbnRfaWQiOiJwaWciLCJ0aW1lX291dCI6MTgwMDAsImxpY2Vuc2UiOiJtYWRlIGJ5IG04IiwiZ2l0VG9rZW5JbmZyYSI6IjEwMmI4MGI4ZDJhZTAzMjU1MzM1OWEwODkwZWEwNjA5ZGE5YzZjOTk5ZjE5ZDhlMzNlYzhlYWU0NzE0OTQ4NWYiLCJnaXRUb2tlbkNvZGUiOiIxMDJiODBiOGQyYWUwMzI1NTMzNTlhMDg5MGVhMDYwOWRhOWM2Yzk5OWYxOWQ4ZTMzZWM4ZWFlNDcxNDk0ODVmIiwidXNlcl9pZCI6MTAwMDY0ODQsImxvZ2luX3RpbWUiOjE2NDUwODI5NDIyMDMsInNjb3BlIjpbInNlcnZlciJdLCJpbnB1dFVzZXJuYW1lIjoid2FuZ2hvbmd3ZWkiLCJmcm9tIjoiY2FzIiwiZXhwIjoxNjQ1MTE4OTQyLCJkZXB0X2lkIjoxMTg5OCwiY3VzdF9pZCI6IjMyNGMxNzM3ODU2MTQ5ZDE5ZTAyN2FkNDVlODY5YTdjIiwianRpIjoiYzE4ZDIyMTctYTUyYi00MGZkLWE3ZDItZmI3MmQyZGNmOGIzIn0.5sVPqjHFb4WyMdPSuesPUkpfGLOTJ5ZkyXzvl-Cp1oo"
RequestURL="https://www.example.com/admin/$custId/users"
Header1="Authorization: $Authorization"
Header2="Content-Type: application/json; charset=UTF-8"

Usage() {
	if [ -z "$1" ]; then
		echo "Usage: $0 请指定需要操作的文件名!"
		exit 1
	fi
}

AddUser() {
	i=1
	while read line; do
		echo $(date "+%Y-%m-%d %H:%M:%S") "正在读取第 $i 行:$line" | $Log_CMD
		arr=($line)
		Data="{
			\"sysUser\": {
				\"custId\": \"$custId\",
				\"email\": \"${arr[0]}\",
				\"nickname\": \"${arr[1]}\",
				\"password\": \"${arr[2]}\",
				\"phone\": \"${arr[3]}\",
				\"delFlag\": 0,
				\"username\": \"${arr[4]}\",
				\"wxNumber\": null,
				\"qqNumber\": null,
				\"telPhone\": null,
				\"discipline\": \"\",
				\"schoolName\": \"\",
				\"graduateTime\": \"\",
				\"edu\": \"\",
				\"extInfo\": {
					\"certificationPath\": []
				},
				\"skill\": [],
				\"skillInfo\": []
			},
			\"sysUserDepts\": [{
				\"deptId\": \"${arr[5]}\",
				\"primaryFlag\": true
			}],
			\"sysUserRoles\": [{
				\"roleId\": \"${arr[6]}\"
			}]
		}"

		echo $(date "+%Y-%m-%d %H:%M:%S") "开始处理第 $i 行..." | $Log_CMD
		curl -sS -k -X POST -H "$Header1" -H "$Header2" -d "$Data" $RequestURL 2>&1 >>$Log_File
		if [ $? != 0 ]; then
			echo "第 $i 行请求失败,请检查日志记录!"
			exit 2
		else
			echo -e "\n"$(date "+%Y-%m-%d %H:%M:%S") "第 $i 行处理完成!" | $Log_CMD
		fi
		echo $(date "+%Y-%m-%d %H:%M:%S") "---------------------------------------------------------------------------------------------------------------------" | $Log_CMD
		sleep 2
		let i=$i+1
	done <$1
}

Usage $1
AddUser $1
echo $(date "+%Y-%m-%d %H:%M:%S") "所有数据处理完成!" | $Log_CMD
posted @ 2022-01-24 09:36  wanghongwei-dev  阅读(356)  评论(0编辑  收藏  举报