Skip to content

Instantly share code, notes, and snippets.

@marjamis
marjamis / aws_cli_paginator.sh
Created July 2, 2017 07:05
A simple sample of running the AWS CLI which will take consideration of pagination to get all results.
#!/bin/bash -xe
# Below command can be replaced to the required CLI, including with custom JSON output, assuming the NextToken is in the same location.
AWS_CLI_COMMAND="aws elasticbeanstalk list-platform-versions --max-records 100 --query={NextToken:NextToken,PlatformARNs:PlatformSummaryList[*].PlatformArn}"
OUTPUT_FILE="./output-$(date +%s)"
function CLI_call() {
if [ ! -v NEXT_TOKEN ]; then
cli_output=$($AWS_CLI_COMMAND)
else
@marjamis
marjamis / awscli.md
Last active February 4, 2022 12:38
Using the AWS CLI for various credential related activities

awscli

Using an assumed role credentials with environment variables

# Clear any existing ENV credentials
unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
# Assumed the specified role and export the required values
KEYS=$(aws sts assume-role --role-arn $AWS_ROLE_ARN \
 --role-session-name testing --duration 900); \
@marjamis
marjamis / githubGraphQL.sh
Last active September 3, 2020 09:56
Github GraphQL bash script to loop through the results with a sample query.
#!/bin/bash -ex
CURL_COMMAND='curl -X POST https://api.github.com/graphql'
RAW_OUTPUT_FILE="$(tempfile -s ".query.output")"
MINIMUM_PLUS_1_COUNT=20
function API_call() {
if [ ! -v NEXT_PAGE ]; then
api_output=$($CURL_COMMAND -H "Authorization:bearer $GITHUB_TOKEN" -d @query)
else
@marjamis
marjamis / aws-jmespath.sh
Last active December 21, 2023 04:55
JMESPath and JQ Examples
aws ec2 describe-instances --query 'Reservations[].Instances[].[PrivateIpAddress, MetadataOptions]' --instance-ids <ids>
@marjamis
marjamis / linegrabber.sh
Created August 24, 2020 06:37
A probably overly complicated way to have multiple shells at the same time access a file, locked for sequential access, in which each shell would receive one line from that file. An example use-case is having 5 shells that are controlled at once to connect to 5 different ips in a file with the one command, this will output a different ip per she…
#!/bin/bash
FILE="ipsToConnectTo"
lineContents() {
# Checks if a line has a * pointer next to it to denote which is the next line that should be returned.
LN=$(grep --color=never -n \* $FILE | cut -f1 -d\:)
# If there is no * pointer it needs a manual reset to the right starting location. A precaution for how I used it but easily customisable.
if [ "${LN}" == "" ] ; then
@marjamis
marjamis / image-magick-cli.sh
Last active October 29, 2023 12:24
Takes a folder of images and combines them together in rows and columns for a one output image, using the ImageMagick CLI
# Loops through all the .png images in the current folder and assigns their filename to an element of the array
counter=0
for i in `ls -1 ./*.png` ; do
images[$counter]=$i;
let counter=counter+1;
done
# Loops through the images array, based on the ROWS and COLS spec, to determine how many items rows and columns there will be when merging. Plenty of improvements here
ROWS=2
COLS=4
@marjamis
marjamis / git_stats.sh
Created November 23, 2021 02:36
Basic way to build up generating of git stats with bash with JSON output. Just an example...
#!/bin/bash
# echo "## Each files last update, sorted from oldest to newest"
# cd .. && git ls-tree -r --name-only HEAD | while read filename; do echo "$(git log -1 --format="%aI" -- $filename) $filename"; done | sort -rk1
function lastCommit {
git log -1 --format=%cI
}
function addNumber {
@marjamis
marjamis / docker_registry.sh
Last active January 5, 2022 02:00
Sample curl commands for accessing registries
export DOCKER_REGISTRY_USERNAME="marjamis"
export REPOSITORY="test"
# Note: If using the base64 command doesn't work here you can run a docker login with your credentials and then copy the base64 value from the file: ~/.docker/config.json
export TOKEN=$(curl -H "Authorization: Basic <base64 of docker hub username:password>" "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$DOCKER_REGISTRY_USERNAME/$REPOSITORY:pull,push" | jq .)
# Get the manifest of a specific image
curl -LH "Authorization: Bearer $TOKEN" https://index.docker.io/v2/$DOCKER_REGISTRY_USERNAME/$REPOSITORY/manifests/<tag or digest> | jq .
# Get a list of tags for images in this repository
@marjamis
marjamis / Makefile.manual-docker-image
Last active September 27, 2023 23:54
Manual creation of a basic docker image with image and repository manifest generation
.DEFAULT_GOAL := helper
BUILD_TIME ?= $(shell date '+%s')
# Useful for colour coding outputs
TEXT_RED = \033[0;31;1m
TEXT_BLUE = \033[0;34;1m
TEXT_GREEN = \033[0;32;1m
TEXT_PURPLE = \033[0;35;1m
TEXT_NOCOLOR = \033[0m
@marjamis
marjamis / curl_eks.sh
Created January 6, 2022 01:23
Curled EKS endpoint with a Bearer Token and the appropriate public certificate
export CLUSTER="test"
PAYLOAD=$(aws eks describe-cluster --name $CLUSTER --query 'cluster.{CA: certificateAuthority.data,Endpoint: endpoint}')
echo $PAYLOAD | jq -rc .CA | base64 -D > /tmp/public_cert
ENDPOINT=$(echo $PAYLOAD | jq -rc .Endpoint)
curl -v --cacert /tmp/public_cert -H "Authorization: Bearer "$(aws eks get-token --cluster-name $CLUSTER | jq -rc .status.token) $ENDPOINT/api/v1/namespaces/default/pods/
## Additional curl options
curl -X GET --cacert /var/lib/kubelet/pods/<podId>/volumes/kubernetes.io~secret/<kube-proxy token secret>/ca.crt -H "Authorization: Bearer $(cat token)" https://<endpoint IP>:443/api/v1/endpoints
kubectl -n kube-system create serviceaccount kube-dns