Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
mttjohnson / ansible_facts_to_from_json.sh
Last active April 11, 2019 04:16
Ansible facts to from and updating json files
# Display all ansible facts from remote machines
ansible -i inventories/example all -m setup
# Write value to json file
TEMP_PLAYBOOK_FILE="TEMP_PLAY.yml"
PLAYBOOK_CONTENTS=$(cat <<'CONTENTS_HEREDOC'
---
- hosts: all
@mttjohnson
mttjohnson / magento2_sandbox_snippet.sh
Created March 15, 2019 19:16
Magento 2 Sandbox Snippet
# Magento 2 Sandbox Snippet - You can copy and paste into any shell in a Magento root directory
# Extended: https://docs.classyllama.net/disciplines/engineering/magento/sandbox-file-m2
set +H # disable history expansion
PHP_CODE=$(cat <<'PHP_CODE'
<?php
header('Content-type: text/plain');
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
@mttjohnson
mttjohnson / enable_disable_lines_in_env.sh
Last active March 14, 2019 16:53
comment/disable or uncomment/enable lines in magento env.php
# Comment out all load_from_slave entries in env.php
PERL_COMMANDS=$(cat <<'PERL_HEREDOC'
s/^(\s*)(?='load_from_slave')(.*)$/$1#$2/;print
PERL_HEREDOC
)
ENV_PHP=$(cat app/etc/env.php | perl -nle ${PERL_COMMANDS})
echo "${ENV_PHP}" > app/etc/env.php
# Uncomment all load_from_slave entries in env.php
PERL_COMMANDS=$(cat <<'PERL_HEREDOC'
@mttjohnson
mttjohnson / parse_nginx_web_request_access_logs.sh
Last active November 10, 2021 00:00
Parsing nginx web request access logs
# Yesterday's and today's access log
# Filter to only show place order actions submitting payment requests that fail (400) or not
# get a count of the number of events in the logs
cat $(find . -regex '.*/www-prod_backend-access\.log-[0-9]+' -print0) www-prod_backend-access.log \
| grep -E 'POST \/rest\/[A-Za-z0-9_]+\/V1\/guest-carts\/[A-Za-z0-9]*\/payment-information HTTP\/[1-2]\.[0-1]" 400' \
| wc -l
cat $(find . -regex '.*/www-prod_backend-access\.log-[0-9]+' -print0) www-prod_backend-access.log \
| grep -E 'POST \/rest\/[A-Za-z0-9_]+\/V1\/guest-carts\/[A-Za-z0-9]*\/payment-information HTTP\/[1-2]\.[0-1]" [^4]00' \
| wc -l
@mttjohnson
mttjohnson / update_m2_env_script.sh
Created March 11, 2019 19:54
Update Magento 2 env.php from bash shell script
#!/usr/bin/env bash
set -eu
# Add configs values to env.php
echo "Add configs values programmatically to env.php"
set +H # disable history expansion
PHP_CODE=$(cat <<'PHP_CODE'
<?php
$existing_env = include("app/etc/env.php");
@mttjohnson
mttjohnson / dangerous_functions.php
Created March 6, 2019 22:35 — forked from perryholden/dangerous_functions.php
Test File or Folder for Dangerous PHP Functions
<?php
/**
* dangerous_functions.php
* Perry Holden <[email protected]>
*
* Usage: php dangerous_functions.php [type: folder|file] [folder_or_filename]
* Example: php dangerous_functions.php folder vendor/businessname/module-name
*/
if (!isset($argv[1])) {
@mttjohnson
mttjohnson / curl_php_vs_bash.sh
Created March 1, 2019 20:53
matching/debugging a php curl request to using curl in bash in a script or cli
#!/usr/bin/env bash
# These examples provide some simple and quick ways to experiment
# with the differences between PHP curl requests and using the
# curl command from a bash shell or script. The commands are arranged
# so they can be easily copy and pasted into a shell for experimenting.
# Use netcat listening locally for testing running netcat from one
# shell and execute the client requests from a separate shell. This
# can be useful to see newline characters and any additional characters
@mttjohnson
mttjohnson / load_all_sitemap_urls.sh
Created March 1, 2019 20:10 — forked from erikhansen/load_all_sitemap_urls.sh
Load all urls from a sitemap.xml file
#!/bin/bash
# This script crawls all urls in a /sitemap.xml file and loads them, effectively priming the cache
# Usage: ./warm_cache.sh www.example.com
time wget --quiet https://$1/sitemap.xml --output-document - | \
egrep -o "https?://[^<]+" | \
grep $1 | \
grep -v "jpg" | \
xargs -i -d '\n' curl --output /dev/null --silent --write-out '%{http_code} %{time_total}ms %{url_effective} \n' {}
@mttjohnson
mttjohnson / bash_disable_enable_append_to_crontab.sh
Created February 26, 2019 22:34
Bash Disable/Enable/Append to crontab
# Example for CRONTAB_ENTRIES
# This is useful for testing scenarios
set +H # disable history expansion
CRONTAB_ENTRIES=$(cat <<CONTENTS_HEREDOC
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
@mttjohnson
mttjohnson / system_limits.sh
Last active February 25, 2020 10:04
View system open file limits (nofile) and how many file in use by user
# View system limit configurations
cat /etc/security/limits.conf
# There may be overrides to limits in limits.d
cat /etc/security/limits.d/90-nproc.conf
# find active number of file descriptors per user
ps -A x | grep [w]ww-prod | awk '{print $1}' | xargs -I '{}' ls /proc/{}/fd | wc -l
# Alternate count of file decriptors