Posts

AWS SSM Maintenance window shortcuts

To list all enabled maintance windows aws ssm describe-maintenance-windows --filters "Key=Enabled,Values=true" --region <region name>  To disable a maintenance window aws ssm update-maintenance-window   --window-id <"window-id">    --no-enabled --region <region name> To enable a maintenance window aws ssm update-maintenance-window --window-id <"window-id"> --enabled --region <region name>

Remove unused volume?

volumeid=( vol-xxxxxxxxxxxxxxxxxxx vol-xxxxxxxxxxxxxxxxxxx vol-xxxxxxxxxxxxxxxxxxx vol-xxxxxxxxxxxxxxxxxxx vol-xxxxxxxxxxxxxxxxxxx ) COUNTER=0 for (( i=0; i<${#volumeid[@]}; i++ )); do COUNTER=$((COUNTER+1)) echo "++++"; echo $COUNTER". Removing the volume with the Volume ID: " ${volumeid[$i]}; aws ec2 delete-volume --volume-id ${volumeid[$i]}; done

Remove unused snapshot?

  snapshotid=( snap-xxxxxxxxxxxxxxxx snap-xxxxxxxxxxxxxxxx snap-xxxxxxxxxxxxxxxx snap-xxxxxxxxxxxxxxxx ) COUNTER=0 for (( i=0; i<${#snapshotid[@]}; i++ )); do COUNTER=$((COUNTER+1)) echo "++++"; echo $COUNTER". Removing the Snapshot with the Snapshot ID: " ${snapshotid[$i]}; aws ec2 delete-snapshot --snapshot-id ${snapshotid[$i]}; done

List volumes without a tag

  for region_name in `aws ec2 describe-regions  --query "Regions[].{Name:RegionName}" --output text` ; do echo "Checking the region" $region_name;         aws ec2 describe-volumes --filters Name=status,Values=available  --query "Volumes[*].{ID:VolumeId,VolumeState:State}"  --query 'Volumes[?!not_null(Tags[])]' --region $region_name --output table; done;

Find ami creation date ?

  for region_name in `aws ec2 describe-regions  --query "Regions[].{Name:RegionName}" --output text` ; do echo "Checking the region" $region_name;  echo "Checking the region" $region_name >> $region_name.txt     aws ec2 describe-images --region $region_name --owners self --query 'sort_by (Images, &CreationDate)[].{Name: Name, Image: ImageId , CreationDate: CreationDate}[]' --output text |  sed -E 's/\s+/,/g' >> $region_name.txt done;

List instances with Name, Public IP, Platform and Status

for region in `aws ec2 describe-regions --output text | cut -f4` do echo -e "\nListing Instances in region:'$region'..."     aws ec2 describe-instances --query "Reservations[*].Instances[*].{PublicIP:PublicIpAddress,Platform:Platform,Name:Tags[?Key=='Name']|[0].Value,Status:State.Name}"  --filters "Name=instance-state-name,Values=running" "Name=tag:Name,Values='*'"  --output table --region $region done

Add a specific tag to EC2 instances?

COUNTER=0 for instance_id in `aws ec2 describe-instances --query "Reservations[*].Instances[*].{Instance:InstanceId}"  --filters "Name=tag:Name,Values=*<Common_name>*"  --output text --region <region_name>` do let COUNTER++ printf $COUNTER; echo ". Setting up tag for the instance "$instance_id; aws ec2 create-tags --resources $instance_id --region <region_name>   --tags "Key"="<key_that_needs_to_be_added>","Value"="<value_that_needs_to_be_added>" ; done;

Remove specific tags from EC2 instance?

COUNTER=0 for instance_id in `aws ec2 describe-instances --query "Reservations[*].Instances[*].{Instance:InstanceId}"  --filters "Name=tag:Name,Values=*<common_name>*"  --output text --region <region_name>` do let COUNTER++ printf $COUNTER; echo ". Removing the tag for the instance "$instance_id; aws ec2 delete-tags --resources $instance_id --tags Key=<Tag_That_needs_Removal> done;

Security Group Inbound rules list using aws cli

  for region in `aws ec2 describe-regions --query "Regions[].{Name:RegionName}"   --output text` do     :     if [ -z "$(aws ec2 describe-instances --region $region --filter "Name=instance-state-name,Values=running" --query "Reservations[*].Instances[*].[InstanceId, Tags[?Key=='Name'].Value|[0]]"  --output text | awk '{print $1}')" ]; then         echo "There are no EC2 Instances in the region :: "$region     else         echo "++++++++++++++++++++++++++++++++++++++++++++++++++++"         echo "Checking Amazon Instances on the region "$region         echo "++++++++++++++++++++++++++++++++++++++++++++++++++++"     fi     for instance in `aws ec2 describe-instances --region $region --filter "Name=instance-state-name,Values=running" --query "Reservations[*].Instances[*].[InstanceId, Tags[?Key=='Name'].Value|[0]]"  --output text | awk '{print $1}'`     do     

AWS instance id, ami id and creation date using shell script with array usage and for loop.

 region_name=("us-east-1" "eu-north-1" "ap-south-1"  "eu-west-3" "eu-west-2" "eu-west-1" "ap-northeast-3" "ap-northeast-2" "ap-northeast-1" "sa-east-1" "ca-central-1" "ap-southeast-1" "ap-southeast-2" "eu-central-1" "us-east-1" "us-east-2" "us-west-1" "us-west-2") for region in "${region_name[@]}"  do   : array_us_east_1=($(aws ec2 describe-instances --filters Name=instance-state-name,Values=running,stopped --query "Reservations[*].Instances[*].InstanceId" --output text --region $region))   for instance_id in "${array_us_east_1[@]}"  do    :    echo "InstanceId :" $instance_id    ami_id_us_east_1=($(aws ec2 describe-instances --instance-id  $instance_id --region $region  --query Reservations[].Instances[].[ImageId] --output text))     for ami_id in "${ami_id_us_eas

How to extract java stack trace log from tomcat log file?

catalina.out - Tomcat output file. ERROR - The keyword displayed in the log file containing the java stack trace. sed -n '/ERROR/p; //,/^[0-9]\{4\}-/ {/^[0-9]\{4\}-/!p}' catalina.out  Ref : https://www.unix.com/shell-programming-and-scripting/277433-using-grep-finding-stacktraces.html

How to find java heap memory usage ?

jmap -heap 26442 Attaching to process ID 26442, please wait... Debugger attached successfully. Server compiler detected. JVM version is 8.1.045 9.0.4+011 using thread-local object allocation. Garbage-First (G1) GC with 8 thread(s) Heap Configuration:    MinHeapFreeRatio         = 40    MaxHeapFreeRatio         = 70    MaxHeapSize              = 27262976000 (26000.0MB)    NewSize                  = 5452592 (5.1999969482421875MB)    MaxNewSize               = 16357785600 (15600.0MB)    OldSize                  = 5452592 (5.1999969482421875MB)    NewRatio                 = 2    SurvivorRatio            = 8    MetaspaceSize            = 21807104 (20.796875MB)    CompressedClassSpaceSize = 268435456 (256.0MB)    MaxMetaspaceSize         = 10737418240 (10240.0MB)    G1HeapRegionSize         = 8388608 (8.0MB) Heap Usage: G1 Heap:    regions  = 3250    capacity = 27262976000 (26000.0MB)    used     = 25738931416 (24546.557823181152MB)    free     = 1524044584 (1453.4421768188477MB)    94.40

SSL domain expiry - bulk domain list

Hi, This is where you need to check the SSL certificate validity for large number of domains. domainlist.txt - The file which stores the list of domains that we need to check the SSL expiry. In this file, make sure that only one domain name is in each row. i - The variable used here, for each iteration, each domain is checked. for i in `cat domainlist.txt` ; do echo "++++++++++++++++ $i ++++++++++++++++" ; echo | openssl s_client -servername NAME -connect $i:443 2>/dev/null | openssl x509 -noout -dates ; echo --------------------------------------- ; done

Bash basics for personal use

Replace using sed when too many slashes are there. --------------------------------------------------- sed -i -e 's@/home/username/jas/@/home/username/jas/folder/@g' file.sh sed -i -e 's@xxx.xxx.xxx.xxx@xxx.xxx.xxx.xxx@g' file.sh Note : In bash scripting on remote server ssh login, we could experience problems with awk print $ and "`". In such situations, use cut and there will be no such issues. To add double quotes in a line which has words separated by comma. ----------------------------------------------------------------- sed -e 's/"//g' -e 's/[^,]*/"&"/g' -i filename To add double quotes at end of line in a file. ------------------------------------------------- sed -e 's/$/"/' -i filename To add double quotes at beginning of a line in a file. ----------------------------------------------------- sed -e 's/^/"/' -i filename To check a log file between two dates. -------------------------------

Add hosts entry using python

#Task : To add hosts entry to a server hosts file using python script. # To whomsoever wondering why, this is a task which can be easily achieved via shell script, but I went forward with Python to learn python. #Script to change the remote IP across the hosts file. #How to run this script : python scriptname IP_that_we_need_to_set_hosts_entry #Importing system library for passing arguments during runtime to incorporate as a jenkins job. import sys #The function add_remote add the remote entry to the hosts file and shows us the new entry that we added. def add_remote():  with open("/etc/hosts", "a") as myfile:   myfile.write( str(entered_IP) + ' remote_server_hostname' + '\n' )  with open("/etc/hosts", "r") as myfile:   for line in myfile.readlines():    if 'remote_server_hostname' in line:     print(line)              #The function fun checks if the hosts entry for remote is added in the server. [Main function] def fu

Shell script to add a public key to server and provide sudo privillege for that user.

#!/bin/bash #function which contains the ssh public key for user admin to add in the servers and another function to check if key already exists #If you want to add your user key, replace the admin with your username and add your ssh key to the section shown below. ################################################################## admin_keyexist_check() { if grep -q "******ssh key goes here******admin@<serverhostname>" /home/admin/.ssh/authorized_keys; then     echo " The ssh key already exists and is shown below...!!"        grep -i "admin" /home/admin/.ssh/authorized_keys     echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"     echo "Removing this shell script now...!!"     rm -fv $0     exit else     echo " The ssh key doesn't exists...!! We will add it...!!"     echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" fi } ####

EC2 instance listing using python boto3

#Purpose : To list all the AWS EC2 instances in the specified region. #The script assume that you have a working aws cli configured with privilege to access the aws ec2 instances. #!/usr/bin/env python import boto3 import sys import time region_name = raw_input('Enter aws region for which you want to get the details of the EC2 Instance [sample : us-east-1]: ') print (region_name, type(region_name)) time.sleep(5) ec2 = boto3.resource('ec2', globals()['region_name']) for instance in ec2.instances.all():     print instance.id, instance.state, instance.private_ip_address, instance.public_ip_address, instance.tags

AWS Command line Tasks : Create EC2 instance and EC2 instance with 500GB root volume.

To create an EC2 instance from command line. aws ec2 run-instances  --image-id ami-efe09bf8 --count 1 --instance-type m4.4xlarge --key-name mysshkey --security-group-ids sg-126adasd2--subnet-id subnet-148fd971 --block-device-mapping /dev/sda1=:500:false --region us-east-1 --image-id : The AMI ID which we are going to create the instance. --count : The number of instances that we need to create. --instance-type : The type of EC2 instance that we are going to use it. --key-name : The pem that we are going to use to connect to this server. --secrutiy group : The security group this instance is getting used. --subnet-id : The subnet in which we want the ec2 instance. --block device mapping - device:space in gb : to terminate volume [true or false.] To create an EC2 instance with 500GB root volume. aws ec2 run-instances --image-id ami-efe09bf8 --block-device-mappings  '[{"DeviceName":"/dev/sda1","Ebs":{"VolumeSize":500,"DeleteO

AWS command line tasks - Create 500 GB EBS Volume, to attach and detach the volume

To create a volume of 500 GB   aws ec2 --region us-east-1 create-volume --size 500 --availability-zone us-east-1d --volume-type gp2 size : in GB availability zone : This needs to be in the same availability zone as that of the instance that we are going to attach the EBS volume. To attach an EBS volume to the ec2 instance. aws  ec2 attach-volume --volume-id vol-9dfjhsdkfj93 --instance-id i-1234567899oasd  --device /dev/xvdb --region us-east-1 After attaching format the drive and mount it like what we do for a normal hard disk drive. To detach an EBS volume from the ec2 instance. aws ec2 detach-volume --volume-id vol-9dfjhsdkfj93 --region us-east-1

AWS Command line Tasks - Create SSH key pair, security group, Allow connection to a port

We need a working aws cli configured to use the following commands. To create an SSH pem file. aws ec2 create-key-pair --region us-east-1 --key-name mysshkey --output text > mysshkey.pem  region :  The region in which the ssh pem file is getting created. Here we are saving the pem file in our linux machine as the name mysshkey.pem  Modify the permission of the pem file. chmod 400 mysshkey.pem  To create a security group. aws ec2 create-security-group --region us-east-1 --group-name work_project --description "Project_for_work" --vpc-id vpc-12345  group name - It is the name that we are going to give for the security group description - The description that we are going to give for the security group To allow connection to a port aws ec2 --region us-east-1 authorize-security-group-ingress --group-id sg-12345 --protocol tcp --port 22 --cidr 10.100.0.0/16 aws ec2 --region us-east-1 authorize-security-group-ingress --group-id sg-12345 --protocol tcp --p