pondelok 26. októbra 2020

Bash tips I

Inputs to functions:

https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html

Outputs from functions:

https://www.linuxjournal.com/content/return-values-bash-functions

Check for empty variable:

https://www.cyberciti.biz/faq/unix-linux-bash-script-check-if-variable-is-empty/

Keyboard shortcuts:

http://teohm.com/blog/shortcuts-to-move-faster-in-bash-command-line/

streda 14. októbra 2020

Statistical analysis of MySQL/MariaDB slow queries

One-liner:

sudo grep -e '^select' -e '^insert' -e '^update' -e '^delete' /srv/data/mysql/mysql_slow.log | cut -d ' ' -f 1-3 | sort | uniq -c | sort -nr | head

utorok 13. októbra 2020

How to extract raw value from JSON and cut it into pieces by size

Sample data:

{
  "var_name1": {},
  "var_name2": "string",
  "var_name3": "long string",
  "var_name4": 15
}

Fill Bash variable with extracted long string raw value from JSON:

VAR_NAME=`cat file.json | jq --raw-output '.var_name3'`

Then reference "chunks" of data using Bash variable index modifiers, e.g. by 255 characters:

  • ${VAR_NAME:0:255}
  • ${VAR_NAME:255:255}
  • ${VAR_NAME:510:255}
  • ${VAR_NAME:765:255}
  • ${VAR_NAME:1020:255}
One-liner alternative extracting and displaying output in single command line:

VAR_NAME=`cat file.json  | jq --raw-output '.var_name3'` ; echo $VAR_NAME ; echo ${VAR_NAME:0:255} ; echo ${VAR_NAME:255:255} ; echo ${VAR_NAME:510:255} ; echo ${VAR_NAME:765:255} ; echo ${VAR_NAME:1020:255}

utorok 29. septembra 2020

How to configure service principal for automated / scripted logins to Azure CLI

Steps:

  • (check permissions)
    • Azure Active Directory (AD) (AAD) level
    • Azure Subscription level
  • app registration
    • AAD > App registrations > New registration 
    • get tenant (directory) ID and app (client) ID
    • configure authentication ( (select app) > Certificates & secrets )
      • certificate-based (> Certificates > Upload certificate)
      • password-based (> Client secrets > New client secret)
  • app role assignment
    • Subscriptions > (select) > Access control (IAM) > Add role assignment
  • (configure access policies on resources)

Using Azure portal:

https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal

Using Azure CLI:

https://docs.microsoft.com/en-us/cli/azure/create-an-azure-service-principal-azure-cli

Login via Azure CLI (password-based):

az login --service-principal --tenant TENANT_ID  --username APP_ID --password PASSWORD

 Login via Azure CLI (certificate-based):

az login --service-principal --tenant TENANT_ID --username APP_ID --password /path/to/cert

Other sign-in options:

https://docs.microsoft.com/en-us/cli/azure/authenticate-azure-cli