wg_clients.sh 2.4 KB

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