1
0

wg_clients.sh 2.2 KB

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