config-nmcli.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. set -e
  3. function usage() {
  4. echo -e "USAGE:
  5. $(basename $0) --help # 使用帮助
  6. $(basename $0) wifi # 连接wifi
  7. $(basename $0) pxe # pxe IP
  8. $(basename $0) work1 # 工作 IP 1
  9. $(basename $0) work2 # 工作 IP 2
  10. $(basename $0) work3 # 黄河大厦 172.29.157.20
  11. "
  12. exit 1
  13. }
  14. # 判断参数个数
  15. if [ $# -eq 0 ]; then
  16. usage
  17. exit 1
  18. fi
  19. IFACE_LAN=$(LANG=en ip link | awk -F '[ :]' '{print $3}' | grep "^e")
  20. echo "IFACE_LAN=${IFACE_LAN}"
  21. if [ "$1" == "wifi" ]; then
  22. WIFI_SSID=kylin-gcjs-zz
  23. if nmcli device wifi list | grep -q ${WIFI_SSID}; then
  24. nmcli dev wifi connect ${WIFI_SSID} password gcjsqwe123
  25. fi
  26. exit
  27. elif [ "$1" == "work1" ]; then
  28. IPADDR=172.29.220.241
  29. GATEWAY=172.29.221.254
  30. DNS1=114.114.114.114
  31. DNS2=172.20.191.2
  32. elif [ "$1" == "work2" ]; then
  33. IPADDR=172.29.220.242
  34. GATEWAY=172.29.221.254
  35. DNS1=114.114.114.114
  36. DNS2=172.20.191.2
  37. elif [ "$1" == "work3" ]; then
  38. IPADDR=172.29.157.20
  39. GATEWAY=172.29.157.254
  40. DNS1=114.114.114.114
  41. DNS2=172.20.191.2
  42. elif [ "$1" == "pxe" ]; then
  43. IPADDR=10.0.0.10
  44. GATEWAY=10.0.0.1
  45. DNS1=10.0.0.10
  46. DNS2=114.114.114.114
  47. elif [ "$1" == "242" ]; then
  48. IPADDR=172.29.220.242
  49. GATEWAY=172.29.220.242
  50. DNS1=172.29.220.242
  51. DNS2=114.114.114.114
  52. elif [ "$1" == "dhcp" ]; then
  53. echo "自动分配"
  54. fi
  55. if [ ! -s $IPADDR ]; then
  56. # nmcli c modify "有线连接 1" ifname ${IFACE_LAN} ipv4.method manual ipv4.addr ${IPADDR}/16 ipv4.gateway ${GATEWAY} ipv4.dns "${DNS1},${DNS2}"
  57. nmcli c modify "有线连接 1" ifname ${IFACE_LAN} ipv4.method manual ipv4.addr ${IPADDR}/24 ipv4.gateway ${GATEWAY} ipv4.dns "${DNS1},${DNS2}"
  58. nmcli c up "有线连接 1"
  59. fi