1、shell腳本:
#!/bin/bash
#auther:

function recreate() {
# shellcheck disable=SC2162
while read line
do
c_id=$(echo "$line"|awk -F : {print $1})
user=$(echo "$line"|awk -F : {print $2})
# shellcheck disable=SC1073
# shellcheck disable=SC2157
if [ -f "/Users/hhjie/gitlab.hhjie.top/hhjie/hhjie-devops/scripts/rebuild_openvpn_user/ccd/${user}" ]; then
rm -f /Users/hhjie/gitlab.hhjie.top/hhjie/hhjie-devops/scripts/rebuild_openvpn_user/ccd/"${user}"
echo /Users/hhjie/gitlab.hhjie.top/hhjie/hhjie-devops/scripts/rebuild_openvpn_user/ccd/"${user}文件已經刪除"
fi
cat >/Users/hhjie/gitlab.hhjie.top/hhjie/hhjie-devops/scripts/rebuild_openvpn_user/ccd/"${user}" << EOF
ifconfig-push 10.158.99.${c_id} 255.255.255.0
push "route 10.77.10.0 255.255.255.0 vpn_gateway"
push "route 192.178.0.0 255.255.255.0 vpn_gateway"
push "route 10.128.78.0 255.255.255.128 vpn_gateway"
push "route 10.15.107.0 255.255.255.0 vpn_gateway"
push "route 172.18.100.0 255.255.255.0 vpn_gateway"
EOF
echo "$user"賬號已經創建


done < /Users/hhjie/gitlab.hhjie.top/hhjie/hhjie-devops/scripts/rebuild_openvpn_user/name2.txt
}

function main() {
recreate
}

main
2、python腳本:
#!/usr/local/env python3

import os.path


def remove_user():
all_user = os.listdir(ccd_dir)
for u in all_user:
per_file = os.path.join(ccd_dir, u)
if u.endswith(_client):
print(f"跳過刪除${per_file}")
else:
os.remove(per_file)


def create_user():
for i in user_list:
print(i)
c_id = i.split(:)[0]
user = i.split(:)[1]
template_format = user_template.format(c_id)
path_join = os.path.join(ccd_dir, user)
with open(path_join, a) as f:
f.write(template_format)


if __name__ == __main__:
user_list = [
"31:chenhui",
"32:huxinyan",
"33:linhuipeng",
"34:liuchao",
"35:liuzhixing",
"36:sunxiangxiang",
"37:wangzhiwei",
"38:yanmingshun",
"39:zhangdipeng",
"40:zhaotongzhou",
"41:zhujia",
"42:yangjia",
"53:lijiachun",
"56:gaoxirui",
"57:xiekai",
"58:suyingying",
"59:yangke",
"61:zhengchengzhi",
"62:zhangqiang",
"63:shipengyu",
"64:houqinghua",
"65:zhangru",
"66:luoqiangqiang",
"72:hhjie",
]

user_template = """ifconfig-push 10.158.99.{0} 255.255.255.0
push "route 10.77.10.0 255.255.255.0 vpn_gateway"
push "route 192.178.0.0 255.255.255.0 vpn_gateway"
push "route 10.128.78.0 255.255.255.128 vpn_gateway"
push "route 10.15.107.0 255.255.255.0 vpn_gateway"
push "route 172.18.100.0 255.255.255.0 vpn_gateway"
"""
ccd_dir = "/Users/hhjie/gitlab.hhjie.top/hhjie/hhjie-devops/scripts/rebuild_openvpn_user/ccd"

remove_user()
create_user()