Skip to content

Instantly share code, notes, and snippets.

View LozanoMatheus's full-sized avatar
💭
with great changes comes great improvements

Matheus Lozano LozanoMatheus

💭
with great changes comes great improvements
View GitHub Profile
#!/bin/bash
# bash generate random alphanumeric string
#
# bash generate random 32 character alphanumeric string (upper and lowercase) and
NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
# bash generate random 32 character alphanumeric string (lowercase only)
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
@LozanoMatheus
LozanoMatheus / 01-bash_http.sh
Last active August 24, 2023 13:24
A simple request - HTTP request with native Bash and without curl/wget/*
#!/usr/bin/env bash +x
## Do you need to test your exposed app but you don't have curl/wget/* installed?
## The ":" is a built-in command and it works similar to true. Let's say, ":" is more portable than "true"
## Simple and easy
:> /dev/tcp/localhost/22
## Why using timeout? If any side has a firewall and it's dropping the packages, the request will take ~80 seconds to get the timeout.
@LozanoMatheus
LozanoMatheus / install_bash.sh
Last active December 31, 2020 20:29
Installing Bash on Ubuntu / Centos / Alpine with --enable-net-redirections
#!/usr/bin/env sh
LINUX_DISTRO="$(. /etc/os-release ; echo ${ID})"
BASH_VERSION="bash-5.0"
SOURCE_DIR="/var/opt/"
prereqs_centos(){
install_bash
yum install -y -q gcc make &> /dev/null
yum clean all &> /dev/null
@LozanoMatheus
LozanoMatheus / get_script_paths.sh
Last active August 26, 2019 15:02
Bash/Shell - Getting the script paths (full and relative paths)
#!/usr/bin/env bash
RELATIVE_PATH="${0}"
RELATIVE_DIR_PATH="$(dirname "${0}")"
FULL_DIR_PATH="$(realpath "${0}" | xargs dirname)"
FULL_PATH="$(realpath "${0}")"
echo "RELATIVE_PATH->${RELATIVE_PATH}<-"
echo "RELATIVE_DIR_PATH->${RELATIVE_DIR_PATH}<-"
echo "FULL_DIR_PATH->${FULL_DIR_PATH}<-"
@LozanoMatheus
LozanoMatheus / post_toggl_time_entries.sh
Created September 18, 2019 08:02
Bash script to post recurrent time entries on Toogl (e.g.: dailies)
#!/usr/bin/env bash
declare -r MONTH="${1:-"$(date +'%m')"}"
declare -r MY_TOGGL_TOKEN="${MY_TOGGL_TOKEN?"Variable MY_TOGGL_TOKEN not declared"}"
declare -r UTC_NEW="$(TZ=UTC date +'%I')"
declare -ri MY_TIME=$(date +'%H')
declare -ri TZ_DIFF="$(( ${MY_TIME/0} - ${UTC_NEW/0} ))"
## How-to get the Project ID (wid): https://github.com/toggl/toggl_api_docs/blob/master/chapters/projects.md
declare -r PROJECT_ID="${PROJECT_ID?"Variable PROJECT_ID not declared"}"
@LozanoMatheus
LozanoMatheus / K8s_Kind_list.txt
Created July 31, 2020 15:14
Compare Kubernetes Objects versions from two clusters
Deployment
Ingress
CronJob
Service
ServiceMonitor
ConfigMap
Secret
@LozanoMatheus
LozanoMatheus / get_ip.sh
Created November 30, 2020 21:41
Get Linux IP without any tool
#!/usr/bin/env bash
## Get the primary and secundary IPs
awk '/\|--/ && !/\.0$|\.255$/ {print $2}' /proc/net/fib_trie
## Get only the primary IPs
awk '/32 host/ { print i } {i=$2}' /proc/net/fib_trie
@LozanoMatheus
LozanoMatheus / ssh_ec2_ssm.sh
Created December 10, 2020 19:41
Accessing an EC2 via SSH using AWS SSM
aws ssm start-session --target ${Instance_Id}
## or
ssh ${Instance_Id}
@LozanoMatheus
LozanoMatheus / ssh-config
Last active December 11, 2020 09:01
Accessing an EC2 via SSH using AWS SSM
## A basic set up of ~/.ssh/config in your local machine/client
## ssh i-0a1b2c3d4e5f6g7h8i9
Host i-*
User ec2-user
ProxyCommand bash -c "aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"
IdentityFile ~/.aws/test-mlozano001.pem
## In this case, it will search for a running instance that has the Tag:Name set to example-ssh-via-aws-ssm
@LozanoMatheus
LozanoMatheus / cleaning_docker.sh
Created December 17, 2020 22:00
Cleaning Docker images, networks, volumes, etc
## Cleaning Docker images, networks, volumes, etc
function flush_docker() {
[[ "${1}" == "all" ]] && local delete_docker_volumes="--volumes"
docker system df
docker rm -f $(docker ps -qa) &> /dev/null
Docker_Prune_All="$(docker system prune --all ${delete_docker_volumes} --force)"
tail -1 <<< "${Docker_Prune_All}"
}