Search code examples
shellfreeradius

how to monitor if radclient packet disconnection has been successful received


I have the following question:

I would like to execute in a bash script the radclient command to force the user to be disconnected, but how I can monitor the result of packet disconnection if it has received and acknowledged?

I was thinking something:

#!/bin/bash

echo "User-Name=value1,Acct-Session-Id=value2" | radclient -x IP_XXXXXX:1700 disconnect secret

if packet received and acknowledged then exit code (1) else exit code (0)


Solution

  • You can use grep to check for a line in the output like:

    #!/bin/bash
    echo "User-Name=value1,Acct-Session-Id=value2" | radclient -x IP_XXXXXX:1700 disconnect secret | grep -Fq 'rad_recv: Disconnect-ACK packet'
    

    Example:

    bash script.sh
    echo "$?"  ## Can produce 0 or 1 depending on the exit value of `grep`.
    

    If you intend to do the test inside the script of course you can just use if:

    if echo "User-Name=value1,Acct-Session-Id=value2" | radclient -x IP_XXXXXX:1700 disconnect secret | grep -Fq 'rad_recv: Disconnect-ACK packet'; then
        ...
    else
        ...
    fi
    

    Or more readable:

    echo "User-Name=value1,Acct-Session-Id=value2" | radclient -x IP_XXXXXX:1700 disconnect secret | grep -Fq 'rad_recv: Disconnect-ACK packet'
    if [[ $? -eq 0 ]]; then
        ...
    else
        ...
    fi
    

    Besides using grep you can also check for the pattern with == and =~:

    RESULT=$(echo "User-Name=value1,Acct-Session-Id=value2" | radclient -x IP_XXXXXX:1700 disconnect secret)
    [[ $RESULT == *'rad_recv: Disconnect-ACK packet'* ]]
    ...
    

    Or

    [[ $RESULT =~ 'rad_recv: Disconnect-ACK packet' ]]