1
0

wg_clients.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/bash
  2. # 服务器 IP 和 端口
  3. port=$(wg show wg0 listen-port)
  4. serverip=$(curl -4 ip.sb)
  5. host=$(hostname -s)
  6. #定义文字颜色
  7. Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m" && Font="\033[0m"
  8. #定义提示信息
  9. Info="${Green}[信息]${Font}" && OK="${Green}[OK]${Font}" && Error="${Red}[错误]${Font}"
  10. # 转到wg配置文件目录
  11. cd /etc/wireguard
  12. cp wg0.conf conf.wg0.bak
  13. echo -e "${RedBG} 重置 WireGuard 客户端 Peer 数量 ${Font}"
  14. echo
  15. echo -e "${GreenBG} 请输入客户端配置数量 ${Font}"
  16. read -p "请输入数字(3--218): " num_x
  17. if [[ ${num_x} -ge 3 ]] && [[ ${num_x} -le 218 ]]; then
  18. wg_num=OK
  19. else
  20. num_x=5
  21. fi
  22. # 删除原1号配置,让IP和配置号对应; 保留原来服务器的端口等配置
  23. rm /etc/wireguard/wg_${host}_* >/dev/null 2>&1
  24. head -n 13 conf.wg0.bak > wg0.conf
  25. # 修改用户配置数量
  26. for i in `seq 2 250`
  27. do
  28. ip=10.0.0.${i}
  29. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  30. cat <<EOF >>wg0.conf
  31. [Peer]
  32. PublicKey = $(cat cpublickey)
  33. AllowedIPs = $ip/32
  34. EOF
  35. cat <<EOF >wg_${host}_$i.conf
  36. [Interface]
  37. PrivateKey = $(cat cprivatekey)
  38. Address = $ip/24
  39. DNS = 8.8.8.8
  40. [Peer]
  41. PublicKey = $(cat spublickey)
  42. Endpoint = $serverip:$port
  43. AllowedIPs = 0.0.0.0/0, ::0/0
  44. PersistentKeepalive = 25
  45. EOF
  46. cat /etc/wireguard/wg_${host}_$i.conf| qrencode -o wg_${host}_$i.png
  47. if [ $i -ge $num_x ]; then
  48. break
  49. fi
  50. done
  51. # 重启wg服务器
  52. wg-quick down wg0 >/dev/null 2>&1
  53. wg-quick up wg0 >/dev/null 2>&1
  54. wg
  55. cat /etc/wireguard/client.conf
  56. cat /etc/wireguard/wg_${host}_2.conf
  57. cat /etc/wireguard/wg_${host}_3.conf
  58. echo -e "${RedBG} 一键安装 WireGuard 脚本 For Debian_9 Ubuntu Centos_7 ${Font}"
  59. echo -e "${GreenBG} 开源项目:https://github.com/hongwenjun/vps_setup ${Font}"
  60. echo
  61. echo -e "# ${Info} 使用${GreenBG} bash wg5 ${Font} 命令,可以临时网页下载配置和二维码"
  62. echo -e "# ${Info} 使用${GreenBG} bash wgmtu ${Font} 命令,重置客户端数量,设置服务器端MTU数值或服务端口号 "