Skip to content

Instantly share code, notes, and snippets.

@greyhoundforty
Created April 19, 2024 14:39
Show Gist options
  • Save greyhoundforty/af6ab9a2fe66e8ebb83d25cc62ff7772 to your computer and use it in GitHub Desktop.
Save greyhoundforty/af6ab9a2fe66e8ebb83d25cc62ff7772 to your computer and use it in GitHub Desktop.
Random-functions
# usage: get_ubuntu_images VPC_REGION
function get_ubuntu_images() {
ibmcloud login -r "$@" -q >/dev/null 2>&1
vpc_region=$(ibmcloud target --output json | jq -r '.region.name')
printf "%b" "\e[1;34mGetting Ubuntu images in ${vpc_region}:.\e[0m"
printf '\n'
ibmcloud is images --visibility public --output json | jq -r '.[] | select(.status=="available") | select(.operating_system.architecture=="amd64") | select(.name | startswith("ibm-ubuntu")) | .name,.id'
}
# usage: get_windows_images VPC_REGION
function get_windows_images() {
ibmcloud login -r "$@" -q >/dev/null 2>&1
vpc_region=$(ibmcloud target --output json | jq -r '.region.name')
printf "%b" "\e[1;34mGetting Windows images in ${vpc_region}:.\e[0m"
printf '\n'
ibmcloud is images --visibility public --output json | jq -r '.[] | select(.status=="available") | select(.operating_system.architecture=="amd64") | select(.name | startswith("ibm-windows")) | .name,.id'
}
# usage: get_all_public_images VPC_REGION
function get_all_public_images() {
ibmcloud login -r "$@" -q >/dev/null 2>&1
vpc_region=$(ibmcloud target --output json | jq -r '.region.name')
printf "%b" "\e[1;34mGetting all public x86 VPC images in ${vpc_region}:.\e[0m"
printf '\n'
ibmcloud is images --visibility public --output json | jq -r '.[] | select(.status=="available") | select(.operating_system.architecture=="amd64") | .name,.id'
}
# usage: get_all_private_images VPC_REGION
function get_all_private_images() {
ibmcloud login -r "$@" -q >/dev/null 2>&1
vpc_region=$(ibmcloud target --output json | jq -r '.region.name')
printf "%b" "\e[1;34mGetting all private VPC images in ${vpc_region}:.\e[0m"
printf '\n'
ibmcloud is images --visibility private --output json | jq -r '.[] | select(.status=="available") | .name,.id'
}
# mac specific to show hidden files in the Finder
function showFiles() {
defaults write com.apple.Finder AppleShowAllFiles true
killAll Finder
}
# mac specific to not show hidden files in the Finder
function hideFiles() {
defaults write com.apple.Finder AppleShowAllFiles false
killAll Finder
}
# usage: icl VPC_REGION
# mainly used in another script when bouncing between work and personal IBM accounts
function icl() {
ibmcloud login -r "$@" -q > /dev/null 2>&1
ibm_region=$(ibmcloud target --output json | jq -r .region.name)
printf "%b" "\e[1;34mConnected to ${ibm_region}:.\e[0m"
}
# Target IBM Cloud CDE account
function target_cde() {
unset IBMCLOUD_API_KEY;
export IBMCLOUD_API_KEY=$(scrt get ibmcloud_api_key);
icl us-south
ibmcloud target -g CDE
}
function target_personal() {
unset IBMCLOUD_API_KEY;
export IBMCLOUD_API_KEY=$(scrt get personal_ibmcloud_api_key);
icl us-south
ibmcloud target -g Default
}
# Remove an IP from the SSH known_hosts file
# usage: iprem 10.240.0.5
function iprem() {
printf "%b" "\e[1;34mRemoving IP ${@} from known hosts file::\e[0m\n"
ssh-keygen -R "$@" -f ~/.ssh/known_hosts
}
# SSH through a bastion host.
# usage: ssh_tunnel <bastion> <target>
function ssh_tunnel() {
printf "%b" "\e[1;34mSSH tunneling through ${1} to ${2}::\e[0m\n"
ssh -o ProxyCommand="ssh -W %h:%p root@${1}" "root@${2}"
}
# Find the 20 most recently modified directories in the current path
function recentdirs() {
tree -D -L 1 -d | head -n 20
}
# Find the 20 most recently modified directories in our projects folder
function recentprojects() {
export PROJECTS_DIR="${HOME}/Sync/SynologyDrive/current-projects"
tree -D -L 1 -d $PROJECTS_DIR | head -n 20
}
# Perform a gitdiff using bat
batdiff() {
git diff --name-only --relative --diff-filter=d | xargs bat --diff
}
# Get IAM token from IBM IAM API and set to TOKEN variable
function get_iam_token() {
TOKEN=$(curl -s -X POST "https://iam.cloud.ibm.com/identity/token" --header "Content-Type: application/x-www-form-urlencoded" --header "Accept: application/json" --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" --data-urlencode "apikey=${IBMCLOUD_API_KEY}" | jq -r '.access_token')
}
# Get external IP address
function get_external_ip() {
curl -s "https://api.ipify.org?format=json" | jq -r '.ip'
}
# Use awk to grab lines from a file. Usage: grab_lines <start_line> <end_line> <file>
function grab_lines() {
awk NR=="$1",NR=="$2" "$3"
}
function take () {
if [[ $1 =~ ^(https?|ftp).*\.(tar\.(gz|bz2|xz)|tgz)$ ]]
then
takeurl "$1"
elif [[ $1 =~ ^([A-Za-z0-9]\+@|https?|git|ssh|ftps?|rsync).*\.git/?$ ]]
then
takegit "$1"
else
takedir "$@"
fi
}
takeurl() {
local data thedir
data="$(mktemp)"
curl -L "$1" > "$data"
tar xf "$data"
thedir="$(tar tf "$data" | head -n 1)"
rm "$data"
cd "$thedir"
}
takegit() {
git clone "$1"
cd "$(basename ${1%%.git})"
}
takedir() {
mkdir -p "$@" && cd "${@:$#}"
}
function namegen() {
uuidgen | tr '[:upper:]' '[:lower:]'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment