My Code and Command line Snippets

AWS

Enable DHCP via userdata on Windows

1
2
3
4
5
6
7
<powershell>
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration | where{$_.IPEnabled -eq "TRUE"}
Foreach($NIC in $NICs) {
$NIC.EnableDHCP()
$NIC.SetDNSServerSearchOrder()
}
</powershell>

List unattached security groups

1
2
3
4
5
6
7
8
export AWS_DEFAULT_PROFILE="$AWS_PROFILE"
export AWS_DEFAULT_REGION="$AWS_REGION"
awless inspect -i port_scanner --local | grep 'applying on nothing' | awk '{print $2}' | while read line; do
aws ec2 describe-security-groups \
--group-ids $line \
--output text \
--query 'SecurityGroups[].[GroupId, VpcId, GroupName]'
done | tr -s '\t' ',' | sort -t, -k2 -k3

List of Instances behind NAT Gateway

1
2
3
4
5
6
export AWS_DEFAULT_OUTPUT="text"
aws ec2 describe-route-tables \
--query 'RouteTables[?Routes[?NatGatewayId==`<NAT_GATEWAY_ID>`]].Associations[].SubnetId' | tr -s '\t' '\n' | while read line; do
aws ec2 describe-instances \
--query 'Reservations[].Instances[?SubnetId==`'"$line"'`].PrivateIpAddress' | tr -s '\t' '\n'
done

Fetch S3 Bucket Sizes

1
2
3
4
aws s3 ls | awk '{print $NF}' | while read line; do
echo "$line"
aws s3 ls s3://"$line" --recursive | grep -v -E "(Bucket: |Prefix: |LastWriteTime|^$|--)" | awk 'BEGIN {total=0}{total+=$3}END{print total/1024/1024" MB"}'
done | paste -d, - -

Check images in launch configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
echo "LaunchConfig,ImageId,CreationDate"
export AWS_DEFAULT_PROFILE="$AWS_PROFILE"
export AWS_DEFAULT_REGION="$AWS_REGION"
export AWS_DEFAULT_OUTPUT="text"
aws autoscaling describe-auto-scaling-groups --query 'AutoScalingGroups[].LaunchConfigurationName' | tr -s '\t' '\n' | while read LAUNCHCONFIG; do
aws autoscaling describe-launch-configurations --launch-configuration-names "$LAUNCHCONFIG" --query 'LaunchConfigurations[].ImageId' | while read AMI_ID; do
export IMAGE_DETAILS="$(aws ec2 describe-images --image-ids "$AMI_ID" --query 'Images[].[ImageId,CreationDate]' | tr -s '\t' ',')"
if [ -n "$IMAGE_DETAILS" ]; then
echo "$LAUNCHCONFIG,$IMAGE_DETAILS"
else
echo "$LAUNCHCONFIG,N/A,N/A"
fi
done
done

List of instances without Environment tag

1
aws ec2 describe-instances --query 'Reservations[].Instances[?!not_null(Tags[?Key == `Environment`].Value)] | [].[InstanceId]'

Get Instance metric

1
2
3
4
5
6
7
8
9
10
aws cloudwatch get-metric-statistics \
--metric-name CPUUtilization \
--start-time 2019-01-01T00:00:00 \
--end-time 2019-01-15T00:00:00 \
--period 1209600 \
--statistics Average \
--namespace AWS/EC2 \
--output text \
--dimensions Name=InstanceId,Value=<INSTANCE_ID> \
--query 'Datapoints[].Average'

IAM Role last used

1
2
3
4
5
6
export AWS_DEFAULT_OUTPUT=text
aws iam list-roles --query 'Roles[].Arn' | tr -s '\t' '\n' | while read ROLE_ARN; do
export NEW_ARN="$(echo $ROLE_ARN | awk -F/ '{print $NF}')"
export ARN_JOB_ID="$(aws iam generate-service-last-accessed-details --arn $ROLE_ARN)"
aws iam get-service-last-accessed-details --job-id "$ARN_JOB_ID" --query 'ServicesLastAccessed[].[ServiceName, LastAuthenticated]' | tr -s '\t' ',' | sed -e 's/^/,/g' -e 's,^,'"$NEW_ARN"',g'
done

Check if AMI is being used

1
2
3
4
5
6
awless list images --sort created --reverse --columns id,name,created --format csv | grep years | cut -d, -f1 | while read line; do
echo "$line"
export AMI_ID="$line"
aws ec2 describe-instances --query 'Reservations[].Instances[?ImageId==`'"$AMI_ID"'`].InstanceId' --output text | tr -s '\t' '\n'
aws autoscaling describe-launch-configurations --output json --output text --query 'LaunchConfigurations[?ImageId==`'"$AMI_ID"'`]'
done

Unused Launch Configurations

1
aws autoscaling describe-launch-configurations --output text --query 'LaunchConfigurations[].LaunchConfigurationName' | tr -s '\t' '\n' | sort | uniq | grep -v -f <(aws autoscaling describe-auto-scaling-groups --output text --query 'AutoScalingGroups[].LaunchConfigurationName' | tr -s '\t' '\n' | sort | uniq)

Find orphaned snapshots

1
aws ec2 describe-snapshots --owner-ids self --output text --query 'Snapshots[].SnapshotId' | tr -s '\t' '\n' | sort | grep -v -f <(aws ec2 describe-images --owners self --output text --query 'Images[].BlockDeviceMappings[].Ebs.SnapshotId' | tr -s '\t' '\n' | sort)

Change backup and maintenance window of RDS

1
2
3
4
5
6
7
8
export RDS_IDENT="$RDS_NAME"

aws rds modify-db-instance \
--db-instance-identifier "$RDS_IDENT" \
--backup-retention-period 7 \
--preferred-backup-window 21:00-22:00 \
--preferred-maintenance-window wed:22:00-wed:23:00 \
--apply-immediately

Delete insufficient CloudWatch alarms

1
2
3
4
awless list alarms --filter state=insufficient --filter name="<ALARM_PATTERN>" --format csv --columns name,state --no-headers | cut -d, -f1 | while read line; do
echo "$line"
awless delete alarm name=\"$line\" -f --no-sync & sleep 1
done

Empty Target Groups

1
2
3
4
5
6
7
8
9
aws elbv2 describe-target-groups --output text --query 'TargetGroups[].TargetGroupArn' | tr -s '\t' '\n' | while read line; do
echo $line
export INST="$(aws elbv2 describe-target-health --target-group-arn "$line" --query 'TargetHealthDescriptions[].Target.Id')"
if [ ${#line} -ne 0 ]; then
echo "$INST"
else
echo None
fi
done | paste -d, - -

List all Regions and AZs

1
2
3
4
aws ec2 describe-regions | jq -r '.Regions[].RegionName' | while read REGION; do
echo "$REGION"
aws ec2 describe-availability-zones --region "$REGION" --output json | jq -r '.AvailabilityZones[].ZoneName' | paste -s -d, -
done

GCP

GCP Snippet 1

1
GCP Snippet 1

Linux

Epoch Time to Date

1
date -d @1560488400 +"%a, %d %b %Y %T %Z"

Get database sizes in MySQL

1
2
3
4
SELECT table_schema AS "Database", 
ROUND(SUM(data_length + index_length) / 1024 / 1024 / 1024, 2) AS "Size (GB)"
FROM information_schema.TABLES
GROUP BY table_schema;

Get database sizes in PostgreSQL

1
2
3
4
5
6
7
8
9
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
AND C.relkind <> 'i'
AND nspname !~ '^pg_toast'
ORDER BY pg_total_relation_size(C.oid) DESC
LIMIT 20;

Last modified files in Linux

1
find -L "$PATH" -type f -printf "%T@ %Tc %p\n" | sort -n

Get file path from script

1
2
FILE_NAME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/$(basename $0)"
echo "$FILE_NAME"

Create Read Only Ubuntu User

https://gist.github.com/varunchandak/ea71fa2f025248aabc7893279dc12bec


DevOps

Delete build history in jenkins

  1. open https://<JENKINS_URL>/script
  2. 1
    2
    3
    4
    5
    def jobName = "<ENTER_JOB_NAME>"
    def job = Jenkins.instance.getItem(jobName)
    job.getBuilds().each { it.delete() }
    job.nextBuildNumber = 1
    job.save()

Random

Cut Youtube Video using ffmpeg

1
2
3
4
ffmpeg -ss <starting_point> \
-i "$(youtube-dl -g -f 22 <YOUTUBE_URL>)" \
-t <how_much_time_after_starting_point> \
-c copy <output>.mp4

Decrypt PDF on command line

1
qpdf --decrypt --password=<PASSWORD> <PDF_PATH> decrypted-<PDF_PATH>

MacOS trackpad scrolling in vim+iTerm2

1
defaults write com.googlecode.iterm2 AlternateMouseScroll -bool true
undefined undefined undefined undefined undefined undefined