Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

Monday, August 10, 2020

Show count and instance IDs of existing EC2 instances in all regions of AWS:

I'm kicking myself for not posting this earlier in my past life-- but here's a handy shell script that can list the number of instances in each region in AWS.


Show count and instance IDs of existing EC2 instances in all regions of AWS:


Single line:

Set my AWS profile to my personal account rather than work.  
To setup, use aws --profile personal configure   

$ export aws_profile=personal


If the buffer space can handle long entries, you can run this in a single line, otherwise there's a multi line below to run as a script.

    $ for i in $(aws --profile $aws_profile ec2 describe-regions --output json | grep "RegionName" | cut -d '"' -f4 | sort); do export buf=$(aws --profile $aws_profile ec2 --region $i describe-instances --output json | grep InstanceId | cut -d '"' -f4); export buf_num=$(echo $buf | grep -c .); echo "=== $i -> $buf_num instance(s) ===";echo $buf ; done

    Multi Line:

    Good for placing into a script to run, also listed this way for readability:
    1. #!/bin/bash export aws_profile=personal for i in $(aws --profile $aws_profile ec2 describe-regions --output json| grep "RegionName" | cut -d '"' -f4 | sort); do export buf=$(aws ec2 describe-instances \ --profile $aws_profile \ --region $i \ --output json | \ grep InstanceId | \ cut -d '"' -f4); export buf_num=$(echo $buf | grep -c .); echo "=== $i -> $buf_num instance(s) ==="; echo $buf; done

    Sample Output:

    1. === ap-northeast-1 -> 0 instances === === ap-northeast-2 -> 0 instances === === ap-south-1 -> 0 instances === === ap-southeast-1 -> 0 instances === === ap-southeast-2 -> 0 instances === === ca-central-1 -> 0 instances === === eu-central-1 -> 0 instances === === eu-north-1 -> 0 instances === === eu-west-1 -> 0 instances === === eu-west-2 -> 0 instances === === eu-west-3 -> 0 instances === === sa-east-1 -> 0 instances === === us-east-1 -> 0 instances === === us-east-2 -> 0 instances === === us-west-1 -> 0 instances === === us-west-2 -> 1 instances === i-012345678901234ab

    Friday, February 28, 2020

    Backing up dot files in my home folder

    In spending some time with customizing my machine, I have some heavily modified configuration files within my home folder.  As a good measure, here's a way to help backup dot files in your home folder:

    $ tar cvfz my_dotfile_backup-$(date +"%Y-%m-%d").tar.gz --exclude .file_to_exclude --exclude .vscode  ~/\.*

    The above will:
      • c = create a tarball
      • v = verbose
      • f = write the output to the specificed file
      • z = compress the output file with gzip. 
      • Output filename will be "my_dotfile_backup-" + the date the command was run in YYYY-MM-DD format + ".tar.gz"
      • Exclude files matching .file_to_exclude, and .vscode
      • ~ = My home folder
      • \.* Escaped dot character then wildcard (meaning the filename needs to start with a .)
    Here's an example output of when the command above was run:

    $ ls -l my_dot*

    -rw-r--r--  1 arojas  staff  30093272 Feb 28 12:17 my_dotfile_backup-2020-02-28.tar.gz

    Tuesday, September 17, 2019

    Testing Gzipped Tarball in Bash

    Originally sourced from stack exchange. modified a bit:
    https://unix.stackexchange.com/questions/129599/test-tar-file-integrity-in-bash

    Modified from the source link above, the following command will iteratively test each .tgz file within the current directory for errors, and if any are detected, will report an error.

    $ for i in *.tgz; do echo "Testing $i..."; if tar xOfz $i &> /dev/null;  then echo "Error with tarball $i"; fi; done


    Options used:
     x  - Extract from archive
     O  - Write output to standard out instead of disk
     f  - Read input from a file
     z  - Tarball is compressed with Gzip (Some OSes may automatically detect Gzip and not need this option)
     &> /dev/null  -   Redirect both stdout and stderr to /dev/null