Skip to content

Instantly share code, notes, and snippets.

View gmirsky's full-sized avatar

Gregory Mirsky gmirsky

View GitHub Profile
@gmirsky
gmirsky / aws_instance_json.sh
Last active October 22, 2020 20:48
Instance Report JSON
#!/usr/local/bin/bash
XXX=$(aws ec2 describe-instances \
--filters "Name=tag-key,Values=Name" \
--query 'Reservations[*].Instances[*].{Instance:InstanceId, Type:InstanceType, Platform:Platform, Volumes:BlockDeviceMappings[*].Ebs.VolumeId, NetworkInterfaces:PrivateIpAddress,AZ:Placement.AvailabilityZone, Name:Tags[?Key==`Name`]|[0].Value}' \
--output json)
echo "$XXX" > xxx.json
@gmirsky
gmirsky / stopped_server_with_public_ip.sh.sh
Created October 7, 2020 19:32
stopped_servers_with_public_ip
#!/bin/bash
aws ec2 describe-instances \
--filter "Name=instance-state-name,Values=stopped" \
--query "Reservations[*].Instances[*].[PublicIpAddress, Tags[?Key=='Name'].Value|[0]]" \
--output text | grep -v "None"
@gmirsky
gmirsky / find_empty_s3_buckets.sh.sh
Created October 7, 2020 18:13
find_empty_s3_buckets
#!/bin/bash
# Find the sizes of the S3 buckets in your account.
for name in $(aws s3api list-buckets --query 'Buckets[*].Name' --output text); do
size=$(aws cloudwatch get-metric-statistics --namespace AWS/S3 --metric-name BucketSizeBytes --start-time $(date --date="yesterday" +%Y-%m-%d) --end-time $(date +%Y-%m-%d) --period 86400 --statistics Maximum --dimensions Name=BucketName,Value=$name Name=StorageType,Value=StandardStorage --query 'Datapoints[0].Maximum' | sed 's/null/0.0/' | cut -d. -f1)
if [[ $size -eq 0 ]]; then
echo "$name"
fi
done
@gmirsky
gmirsky / find_detailed_unused_aws_amis.sh.sh
Created October 7, 2020 15:20
find_detailed_unused_aws_amis
#!/bin/bash
# Find self-owned AMI's which are not used by any instances or launch
# configurations in the default region.
#
# Output with details in descending order by name, creation date
aws ec2 describe-images --image-ids \
$(comm -23 \
<(aws ec2 describe-images --owner self --query 'Images[].[ImageId]' --output text | sort) \
<((
aws ec2 describe-instances --query 'Reservations[].Instances[].[ImageId]' --output text
@gmirsky
gmirsky / find_unused_aws_amis.sh.sh
Created October 7, 2020 15:15
find_unused_aws_amis
#!/bin/bash
# Find self-owned AMIs that are not used by any instances
# or any launch configurations in the default region.
comm -23 \
<(aws ec2 describe-images --owner self --query 'Images[].[ImageId]' --output text | sort) \
<((
aws ec2 describe-instances --query 'Reservations[].Instances[].[ImageId]' --output text
aws autoscaling describe-launch-configurations --query 'LaunchConfigurations[].[ImageId]' --output text
) | sort -u)
@gmirsky
gmirsky / SearchADUser.ps1.ps1
Created August 31, 2020 14:12
Search for an AD user
import-module ActiveDirectory
$Search_Criteria = "*SMPTest.*"
$AD_Server = "dc1.msad.local"
Get-ADUser -Server $AD_Server -Filter 'SamAccountName -like $Search_Criteria' -Properties Enabled, whencreated, whenchanged, EmailAddress, Company, LockedOut,Description, MemberOf | <# Filter on Extended Property 'Enabled' for all Active users #> where-object enabled -eq $True| Select-Object Name, whencreated,whenchanged, SamAccountName, EmailAddress, Company, Description,LockedOut,MemberOf, Enabled | Format-Table -Wrap Name,whencreated,whenchanged,SamAccountName, EmailAddress, Company, Description,LockedOut, Enabled, MemberOf -A
@gmirsky
gmirsky / Mount-New-SmbGlobalMapping.ps1
Last active October 7, 2020 19:52
Mounts a SMB Global Mapping path with the supplied credentials
<#
.SYNOPSIS
Mounts a SMB Global Mapping patth with the supplied credentials
.DESCRIPTION
Mounts a Global SMB Mapping path to the server this script is run on.
Usually used on servers that host docker containers that require
persistent data storage that resides on a network resource.
@gmirsky
gmirsky / AwsIamPolicyS3Directory.json
Last active October 7, 2020 19:52
AWS IAM Policy to limit user to a directory in a bucket
{
"Version": "2012-10-17",
"Statement":[
{
"Sid":"AllowListBucketIfSpecificPrefixIsIncludedInRequest",
"Action":["s3:ListBucket"],
"Effect":"Allow",
"Resource":["arn:aws:s3:::my-bucket"],
"Condition":{
"StringLike":{"s3:prefix":["prod/*"]
@gmirsky
gmirsky / AwsOrphanedEbsSnapshots.ps.sh
Last active October 7, 2020 19:53
AwsOrphanedEbsSnapshots
#!/bin/sh
# remove x if you don't want to see the commands
set -ex
# Some variable initialisation with sane defaults
DRUN='--dry-run'
DO_DELETE=${1:-'no'}
REGION=${2:-'eu-west-1'}
ACCOUNTID=${3:-'self'}
@gmirsky
gmirsky / AwsListUnusedKeyPairs.ps1.ps1
Created July 22, 2020 20:06
AWS List Unused Key Pairs
Param( $AWSRegion = 'us-east-1', $AWSProfileName = 'default')
#
# Store your AWS credentials using the following Powershell AWS command:
#
# Set-AWSCredentials -AccessKey {xx} -SecretKey {xx} -StoreAs {MyProfileName}
#
# Example:
#
# Set-AWSCredentials -AccessKey 'AKIAVJL...KWJKOB4XN' -SecretKey 'oxLcrpnd3S+...1Y5eg3m92E8e2Me' -StoreAs 'testprofile'
# Set-DefaultAWSRegion us-east-1