wg_clients.sh 2.3 KB

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