使用账号密码登录的Openvpn,使用shell进行管理 对分配的IP进行查找重复等
交互式  傻瓜式的,易用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 #!/bin/bash CDD_DIR=/etc/openvpn/ccd DIR=/etc/openvpn PASS_FILE=$DIR /pass_file function  list_used_ip {    rm  -rf "$DIR /ip_list.txt"      find "$CDD_DIR "  -type  f | while  read  -r file; do           awk -v filename="${file##*/} "  '{print $2,filename}'  "$file "  >> "$DIR /ip_list.txt"      done      sort  -n -k1,1 -t' '  "${DIR} /ip_list.txt"  > "${DIR} /ip_list_sort.txt"  } while  true do     list_used_ip     echo  "      ==========================分配openvpn账号密码脚本==========================     1.列出已使用ip列表     2.添加新车辆与ip     ========================================================================     "     read  -p "请输入数字或exit退出脚本:"  num     case  $num  in          1)             echo  "已使用ip列表:"              cat  "$DIR /ip_list_sort.txt"              ;;         2)             read  -p "请输入车辆名称:"  car_name                          if  [[ ! $car_name  =~ ^[A-Za-z0-9]+$ ]]; then                  echo  "车辆名含有非法字符,请重新输入"                  continue              fi                           if  [[ ! $car_name  =~ ^car ]]; then                  car_name="car$car_name "              fi              read  -p "请输入分配的ip地址:"  car_ip                          if  [[ ! $car_ip  =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then                  echo  "ip地址不合法,请重新输入"                  continue              fi                           if  [[ $car_ip  != 192.168.1.* ]]; then                  echo  "ip地址网段不合法,请重新输入"                  continue              fi                           if  grep -q "$car_ip "  "$DIR /ip_list_sort.txt" ; then                  echo  "该ip已存在,请查询后重新输入"                  continue              else                  echo  "该ip可用                  请确认车辆名和ip地址:                 车辆名为$car_name                  ip地址为$car_ip                  输入yes确认,输入no重新输入"                 read  -p "请输入yes或no:"  confirm                 if  [[ $confirm  == "yes"  ]]; then                                           sed -i "/^${car_name}  123456$/d"  "$PASS_FILE "                      echo  "$car_name  123456"  >> "$PASS_FILE "                                           rm  -rf "$CDD_DIR /$car_name "                      echo  "ifconfig-push $car_ip  255.255.255.0"  >> "$CDD_DIR /$car_name "                      echo  "添加成功"                  else                      echo  "重新输入"                      continue                  fi              fi              ;;         "exit" |exit )             break              ;;         *)             echo  "无效,请重新输入"              ;;     esac  done 
 
非交互式传参 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 #!/bin/bash CDD_DIR=/etc/openvpn/ccd DIR=/etc/openvpn PASS_FILE=$DIR /pass_file function  list_used_ip {    rm  -rf "$DIR /ip_list.txt"      find "$CDD_DIR "  -type  f | while  read  -r file; do           awk -v filename="${file##*/} "  '{print $2,filename}'  "$file "  >> "$DIR /ip_list.txt"      done      awk '{print $2, $1}'  "${DIR} /ip_list.txt"  | sort  -n -t. -k1,1 -k2,2 -k3,3 -k4,4 | awk '{print $2, $1}'  > "${DIR} /ip_list_sort.txt"  } list_used_ip case  $1  in     "list" )         echo  "已使用ip列表:"          cat  "$DIR /ip_list_sort.txt"          ;;     "add" )         if  [ $#  -ne 3 ]; then              echo  "缺少参数,请输入车辆名和ip地址"              exit  1         fi                   if  [[ ! $2  =~ ^[A-Za-z0-9]+$ ]]; then              echo  "车辆名含有非法字符,请重新输入"              exit  1         fi                   if  [[ ! $3  =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then              echo  "ip地址格式不合法,请重新输入"              exit  1         fi                   if  [[ $3  != 192.168.1.* ]]; then              echo  "ip地址网段不合法,请重新输入"              exit  1         fi                   if  grep -q -w "$3 "  "$DIR /ip_list_sort.txt" ; then              echo  "该ip已存在,请查询后重新输入"              exit  1         fi                   if  [[ ! $2  =~ ^car ]]; then              car_name="car$2 "              sed -i "/^${car_name}  123456$/d"  "$PASS_FILE "              echo  "${car_name}  123456"  >> "$PASS_FILE "              rm  -rf "$CDD_DIR /$car_name "              echo  "ifconfig-push $3  255.255.255.0"  >> "$CDD_DIR /$car_name "              echo  "添加成功"              exit  0         fi                   sed -i "/^$2  123456$/d"  "$PASS_FILE "          echo  "$2  123456"  >> "$PASS_FILE "                   rm  -rf "$CDD_DIR /$2 "          echo  "ifconfig-push $3  255.255.255.0"  >> "$CDD_DIR /$2 "          echo  "添加成功"          ;;     "help" )         echo  "./openvpn-manager.sh <parameter>                      list                   -- show add used ip                     add <car_name> <ip>    -- add car name and ip                     --help                 -- show help                     "         ;;     *)         echo  "unknown parameter          --help -- show help"         ;;     esac