Easily manage RADIUS servers with Linux shell scripts

Source: Internet
Author: User

The company's wireless environment uses the MAC address authentication method, the MAC address is bound to the radius of the users configuration file, the registered MAC address as the user name and password. In order to manage these MAC addresses conveniently, I wrote a shell script to manage it.

The combination of powerful text processing and various command functions that are unique to shell scripting makes it easy for administrators to work.

The following is a list of the functions of the script as a reference:

    1. Add MAC Address

    2. Delete MAC Address

    3. Find MAC Address

    4. Remove duplicate MAC Address

    5. Check MAC address legitimacy

    6. TODO, import and Export MAC address, add comment

The shell scripting techniques used include, but are not limited to:

    1. column handling and row processing of text files, such as SED, awk, and more

    2. String lookup, filtering, case conversion, bash and grep commands

    3. Get, calculate, compare string lengths, bash and WC commands

    4. Handling and type conversion of regular expressions for Mac addresses

    5. Shell programming operations, including file inclusions, functions, parameter passing, return values, etc.

    6. Other

code example:

#!/bin/bash## source function library.  /etc/rc.d/init.d/functionsradiusd=/usr/sbin/radiusdlockf=/var/lock/subsys/radiusdconfig=/etc/raddb/ radiusd.confuserconfig=/etc/raddb/users[ -f  $RADIUSD  ] | |  exit 0[ -f  $CONFIG  ] | |  exit 0[ -f  $USERCONFIG  ] | |  exit 0retval=0operation=$1macaddress=$2function help () {clearecho $ "" echo $ "========= =========================================================================== "echo $" For Radius  On fedora/centos/radhat linux server, written by chris "echo $" ============= ======================================================================= "echo $" A tool to  Manage radius server "echo $" "echo $" usage: $0 {find|add|modify|delete|check| Remove|start|stop|status|restart|reload} mac "#TODOecho  $" Usage: $0 {import|export|debug}"echo $" "echo $" for more information please contract [email protected] . com "echo $" ==================================================================================== "Echo  $ "" Exit 1}function mac () {if [ -z  $MACADDRESS  ];thenecho $ "No mac  address is signed!  "echo $" \$2 is  $MACADDRESS "exit 1else                 if [[  "${# MACADDRESS} " != "  ]] && [[  "${#MACADDRESS}"  !=  "&nbsp";]  ;thenecho  "mac length is ${#MACADDRESS}"                          echo   "mac address is illegal! " exit 1#                 else#                         echo $ "mac which you  input is  $MACADDRESS "                 fi#echo  $MACADDRESS  | sed -nr  '/[a-fa-f0-9]{2}:[a-fa-f0-9]{2}:[ a-fa-f0-9]{2}:[a-fa-f0-9]{2}:[a-fa-f0-9]{2}:[a-fa-f0-9]{2}/p ' #echo   $MACADDRESS  | SED -NR   '/[a-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}/p ' #echo   $MACADDRESS  | sed -nr  '/[a-fa-f0-9]{12}/p ' if [[  ' echo  $MACADDRESS  | grep -'  ]];thenpromac= ' echo  $MACADDRESS  | sed -nr  '/[a-fa-f0-9 ]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}-[a-fa-f0-9]{2}/p '  | tr  ' [: Upper:] '   ' [: Lower:] '  | sed  ' s/-//g ' elif [[  ' echo  $MACADDRESS  | grep : '  ]];thenpromac= ' echo   $MACADDRESS  | sed -nr  '/[a-fa-f0-9]{2}:[a-fa-f0-9]{2}:[a-fa-f0-9]{2}:[a-fa-f0-9]{2}: [A-fa-f0-9] {2}:[a-fa-f0-9]{2}/p '  | tr  ' [: Upper:] '   ' [: Lower:] '  | sed  ' s/://g ' Elsepromac= ' echo  $MACADDRESS  | tr  ' [: Upper:] '   ' [: Lower:] ' fiecho  $PROMACfi} Function find () {mac= ' MAC ' echo $ "accepted mac is  $MAC" if [[  ' grep  $MAC   $USERCONFIG '  ]]; thenmacline= ' grep -n  $MAC   $USERCONFIG  | awk -f   ': '   ' {print $1} ' #echo   $MACLINEMACLINECOUNT =$ (echo  $MACLINE  | wc -w) # echo  $MACLINECOUNTif  [[  "$MACLINECOUNT"  !=  "1"  ]];thenecho $ "error,  this mac  $MAC  has duplicate record, you should use $0 remove   $MAC  to remove duplicate record "exit 1fiecho $" successfully find  $MAC  in   $MACLINE  line of file  $USERCONFIG!  "echoreval=$?elseecho $" Can not  find  $MAC  in file  $USERCONFIG!  "Echoexit 1reval=$?fi}function add () { Mac= ' mac ' echo $ "accepted mac is  $MAC" #find   $MACLINENUM = ' grep -n  ' Cleartext-password := ' " users | grep -v \# | head -n1 |  awk -F  ":"   ' {print $1} ' sedoperation= $LINENUM "a" sed -i  "$SEDOPERATION   $MAC     cleartext-password := ' $MAC '   $USERCONFIGfind   $MACrestart} Function modify () {mac= ' MAC ' find  $MAC #todo}function delete () {         mac= ' MAC '         echo $ "accepted mac  is  $MAC "        if [[  ' grep  $MAC   $USERCONFIG '  ]]; then                 macline= ' grep -n  $MAC  $ userconfig | awk -f  ': '   ' {print $1} '                  # #echo   $MACLINE                   #MACLINECOUNT =$ (echo  $MACLINE  | WC  -W)                 ## echo  $MACLINECOUNT                   #if  [[  "$MACLINECOUNT"  !=  "1"  ]];then                 #         echo $ "error, this mac  $MAC  has duplicate record, you should use $0 remove   $MAC  to remove duplicate record "                 #        exit 1                  #fi                  echo $ " successfully find  $MAC  in  $MACLINE  line of file  $USERCONFIG!  " echo $ "it will be deleted! " sed -i  "$MACLINE  d"   $USERCONFIG # todoecho $ "if you see  ' can not find  $MAC  in file  $USERCONFIG !  ', it means successfully!  ' find  $MAC                  echo                 REVAL=$?        else                 echo $ "can not find  $MAC   in file  $USERCONFIG!  "                 echo                 reval=$?        fi}function check () {MAC= ' MAC ' find  $MACremove   $MAC}function remove () {mac= ' MAC '          echo $ "accepted mac is  $MAC" #TODO #echo $ "Backuped file to file   $FILENAME '         if [[  ' grep  $MAC  $ Userconfig '  ]]; THEN &NBSp;              macline= ' Grep -n   $MAC   $USERCONFIG  | awk -F  ': '   ' {print $1} '                   #echo   $MACLINE                  maclinecount=$ (echo $ MACLINE | WC -W)                   #echo   $MACLINECOUNT                  if [[  "$MACLINECOUNT"  ==  "1"  ]];then                          echo $ "warnning, this mac  $MAC  is good record, no  duplicate record has found!  "                         exit 0                 fitoremove= "$MAC      cleartext-password := ' $MAC ' "sed -i "/^ $TOREMOVE $/d "  $USERCONFIGadd   $MACFI}function  restart () {service radiusd restart}function reload () {service radiusd reload} Function status () {service radiusd status}case  "$"  in find) findRETVAL=$?;; Add) addretval=$?;; Modify) modifyretval=$?;; Delete) deleteretval=$?;; Check) checkretval=$?;; Remove) removeretval=$?;; Start) startretval=$?;; stop) stopretval=$?;; status) statusretval=$?;; restart) restartretval=$?;; Reload) reloadretval=$?;; *) helpexit 1;; Esac

Some of them can be improved, such as a different method or enhance the user experience is possible, welcome comments.

This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1567085

Easily manage RADIUS servers with Linux shell scripts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.