Posts Using curl and grep to select currently active endpoint
Post
Cancel

Using curl and grep to select currently active endpoint

Let’s say there are two servers, and we need to select the currently active one.

  • Server IP are

    10.0.105.114

    10.0.105.115

  • API to get active endpoint is /api/HA
  • API base path is /api/open/v1

    if the server is active, then the response will contain json key as ‘Role’ and value as ‘Active’

  • We use the linux CURL to get response and filter output using grep command
1
2
3
4
5
6
$ curl -s https://10.0.105.114/api/HA | grep -iq "Role.:.Active" &&
ACTIVE_ENDPOINT=https://10.0.105.114/api/open/v1 ||
ACTIVE_ENDPOINT=https://10.0.105.115/api/open/v1

$ echo $ACTIVE_ENDPOINT
https://10.0.105.114/api/open/v1
  • The above command uses curl then pipes/passes the output to grep
  • grep uses i for ignore case when searching for text “Role.:.Active”
  • grep uses q for quiet, meaning do not write anything on standard-output/terminal
  • && basically does the if true then use the current server, meaning else use the other server
  • print the selected endpoint saved in the ACTIVE_ENDPOINT variable by using echo $ACTIVE_ENDPOINT
This post is licensed under CC BY 4.0 by the author.