Skip to content

Instantly share code, notes, and snippets.

@tjsullivan1
tjsullivan1 / get_recent_ami.sh
Last active March 19, 2018 22:06
Based off of this guide: https://access.redhat.com/solutions/15356, this script will get you the info on AMIs matching different criteria for a current image that you want.
#!/bin/bash
: ${1?"Usage: $0 RHEL||Amazon||Ubuntu"}
if [ $1 == "RHEL" ]; then
# RHEL is slightly different so we don't sort by creation date. Instead, we look for the number after x86_64-, which indicates Kernel revision. Higher is better.
aws ec2 describe-images --owner 309956199498 --filters 'Name=name,Values=RHEL-7.4_HVM-201?????-x86_64-?-Hourly2-GP2' --query 'Images[].[ImageId,Name,CreationDate,OwnerId]' --output text | sed 's/\(\S*\)\(.*\)/\2 \1/' | awk -F'x86_64-' '{print $2}' | sort | tail -n 1 | awk -F'ami-' '{print "ami-"$2}'
elif [ $1 == "Amazon" ]; then
aws ec2 describe-images --owner 137112412989 --filters 'Name=name,Values=amzn2-ami-hvm-2017.12.0.201?????-x86_64-gp2' --query 'sort_by(Images, &CreationDate)[-1].[ImageId]' --output text
elif [ $1 == "Ubuntu" ]; then
@tjsullivan1
tjsullivan1 / send_ses_email.py
Last active October 27, 2017 04:13
Modified from here: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-using-sdk-python.html -- requires a credential profile called "email" with region defined.
#! python3
def sendMessage(message_subject, sender, recipient, html_body, text_body, charset="UTF-8"):
import boto3
from botocore.exceptions import ClientError
# Load shared configuration file
session = boto3.Session(profile_name='email')
# Create a new SES resource and specify a region.
@tjsullivan1
tjsullivan1 / quick_web_search.py
Created October 7, 2017 03:15
A function that will search for a regular expression in a url
import requests, bs4, re
def check_url(url, regex):
res = requests.get(url)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, "lxml")
found_string = soup.body.findAll(text=re.compile(regex), limit=1)
if found_string:
@tjsullivan1
tjsullivan1 / Export-SqlInfo.ps1
Created September 13, 2017 20:32
This is a series of scripts that will get your sql edition, sql version, core count, and socket count. Useful for MSFT renewals.
Get-Content C:\Users\tjsullivan1\Documents\computers.txt | % {
$server = $_
$SQLAttrs = Get-SqlServerInfo -ComputerName $server | select SQLVersion,SQLEdition,Hostname
$ProcInfo = Get-ProcCoreSocketCount -ComputerName $server
$SQLVersion = $SQLAttrs.SQLVersion
$SQLEdition = $SQLAttrs.SQLEdition
$Cores = $ProcInfo.Cores
$Sockets = $ProcInfo.Sockets
$FullInfo = [PSCustomObject]@{
@tjsullivan1
tjsullivan1 / change_twiki_machine_name.sh
Created August 21, 2017 14:48
This is one of the better representations I have of populating a twiki field name with sed. A variable could be used in the sed pattern to allow us to replace a value as well. It is important to take more from the line in twiki than just what we are replacing, in order to ensure we don't overwrite multiple values.
#!/bin/bash
file='twiki_vms.csv'
while IFS="," read servername vm
do
twikifile='/var/www/html/twiki/data/ITOperations/'$servername'.txt'
echo $vm
cat $twikifile | grep MachineName
sed -i s"/name=\"MachineName\"\stitle=\"Machine\sName\"\svalue=\"\"/name=\"MachineName\" title=\"Machine Name\" value=\"$vm\"/" $twikifile
@tjsullivan1
tjsullivan1 / New-ADAWSRoleGroup.ps1
Created June 21, 2017 02:07
Create a group in AD for usage with a SAML config
[cmdletbinding()]
param(
[Parameter(Mandatory=$true,HelpMessage="Remember, this must match the name part after the accountID of the AD group you are using!")]
[string]$Role_Name,
$Account_Number = '123456789012'
)
$Name = "AWS-$Account_Number-$Role_Name"
@tjsullivan1
tjsullivan1 / install_hashi_tools.py
Last active April 19, 2018 21:27
This tool will download the binaries for Packer, Terraform, Vault, Consul, and Nomad. Vagrant was not included because the install isn't a binary. This works on both Linux and Windows.
#! python3
import os, urllib.request, shutil, logging, zipfile, stat
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh = logging.FileHandler('install_hashi_tools.log')
[cmdletbinding()]
param(
[Parameter(HelpMessage="Remember, this must match the name part after the accountID of the AD group you are using!")]
[string]$Role_Name = "Admin",
$Policy_Name
)
$AWS_Account_ID = Get-EC2SecurityGroup -GroupName "default" | select -ExpandProperty OwnerID # this grabs Owner ID from the default VPC security group
$account_arn = 'arn:aws:iam::'+$AWS_Account_ID+':saml-provider/ADFS'
@tjsullivan1
tjsullivan1 / New-SAMLConfiguration.ps1
Last active June 12, 2017 15:42
Create a SAML configuration in AWS/ADFS
[cmdletbinding()]
param(
[string]$ADFS_RelyingParty_Trust_Name = 'AWS',
[string]$ADFS_Metadata_URL = 'https://<ADFS_SERVER_FQDN>/FederationMetadata/2007-06/FederationMetadata.xml',
[string]$IDP_Provider_Name = 'ADFS',
[Parameter(HelpMessage="Remember, this must match the name of the AD group you are using!")]
[string]$Role_Name = "AWS-Admin"
@tjsullivan1
tjsullivan1 / Get-ReverseLookupNoPTR.ps1
Created April 7, 2017 15:32
If your DNS configuration is bad, and you have A records without PTRs, you can easily perform a lookup to find out what that device is.
param(
$Zone_Name,
$DNS_Server,
$IP
)
Get-DnsServerResourceRecord -ZoneName $Zone_Name -ComputerName $DNS_Server |
Where { $_.RecordType -eq 'A' -and $_.RecordData.IPv4Address.IPAddressToString -like "$IP"}