Skip to content

Instantly share code, notes, and snippets.

@colegatron
colegatron / AWS EC2 dinamically add 2nd ENI to instance
Created December 9, 2015 16:23
AWS EC2 dinamically add 2nd ENI to instance (and only a 2nd
INSTID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
echo "Getting instance info..."
aws ec2 describe-instances --instance-id $INSTID --region eu-west-1 | jq .Reservations > /tmp/descinstance.json
if [ $( cat /tmp/descinstance.json | jq '.[].Instances[0].NetworkInterfaces | length' ) -gt 1 ]; then
echo "[WARNING] Instance already has more than one ENI"
else
echo "Creating ENI..."
aws ec2 create-network-interface --subnet-id subnet-14416563 --description "Nic2 SaltMaster DevTest Network [$INSTID]" --groups sg-28dc214d --region eu-west-1 > /tmp/create-eni.json
echo "Getting ENI id..."
NICID=$(cat /tmp/create-eni.json | jq .NetworkInterface.NetworkInterfaceId | sed "s/\"//g")
# install 7-zip, curl and vim
# (Windows 2012 comes with .NET 4.5 out-of-the-box)
# Then use the EC2 tools to create a new AMI from the result, and you have a system
# that will execute user-data as a PowerShell script after the instance fires up!
# This has been tested on Windows 2012 64bits AMIs provided by Amazon (eu-west-1 ami-a1867dd6)
#
# Inject this as user-data of a Windows 2012 AMI, like this (edit the adminPassword to your needs):
#
# <powershell>
# Set-ExecutionPolicy Unrestricted
@colegatron
colegatron / ListAttachedEBSVolumes
Created July 17, 2015 11:02
AWS List EBS volumes attached to a Windows instance with powershell
# List the Windows disks
# Create a hash table that maps each device to a SCSI target
$Map = @{"0" = '/dev/sda1'}
for($x = 1; $x -le 26; $x++) {$Map.add($x.ToString(), [String]::Format("xvd{0}",[char](97 + $x)))}
for($x = 78; $x -le 102; $x++) {$Map.add($x.ToString(), [String]::Format("xvdc{0}",[char](19 + $x)))}
Try {
# Use the metadata service to discover which instance the script is running on
$InstanceId = (Invoke-WebRequest '169.254.169.254/latest/meta-data/instance-id').Content
@colegatron
colegatron / Recursively find folders named "xml_contents" and delete them
Last active August 29, 2015 14:18
Find folder named "Content" and delete them recursively
$folderToRemove = "Contents"
$rootFolder = "e:\import_logs"
$folders = Get-ChildItem -Recurse -Force $rootFolder -ErrorAction SilentlyContinue | Where-Object { ($_.PSIsContainer -eq $true) -and ( $_.Name -like $folderToRemove) }
foreach ($folder in $folders) {
write-host "Removing " $folder.FullName
remove-item -re -fo $folder.FullName
}
-- Only procced with deletion if the there are at least @MINIMUM_IN_DB records for the last @NDAYS
DECLARE @NDAYS int = 3 -- Number of days to preserve
DECLARE @MININUM_IN_TABLE int = 2100000 -- Records needed in the DB for the last @NDAYS to proceed to deletion
DECLARE @TODELETE int = 1000 -- Delete un chunks of @TODELETE records
DECLARE @TOTALRECS int = ( select count(*) from intermediate where datediff(day, insertdate,getdate()) < @NDAYS )
IF ( @TOTALRECS >= @MININUM_IN_TABLE )
BEGIN
@colegatron
colegatron / gist:10723614
Created April 15, 2014 11:11
IIS6 regexp log
((?# date)\d{4}\-\d{2}\-\d{2}\s+)((?# time)\d{2}\:\d{2}\:\d{2}\s+)((?# s-sitename).+?\s+)((?# s-computername).+?\s+)((?# s-ip)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s+)((?# cs-method).+?\s+)((?# cs-uri-stem).+?\s+)((?# cs-uri-query).+?\s+)((?# s-port)\d{1,3}\s+)((?# cs-username).+?\s+)((?# c-ip)\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\s+)((?# cs-version)(HTTP\/\d\.\d)|\-\s+)((?# csUser-Agent).+?\s+)((?# csCookie).+?\s+)((?# csReferer).+)((?# cs-host).+?\s+)((?# sc-status)\d{1,3}\s+)((?# sc-substatus)\d{1,3}\s+)((?# sc-win32-status)\d+\s+)((?# sc-bytes)\d+\s+)((?# cs-bytes)\d+\s+)((?# time-taken)\d+)
Credits to http://weblogs.asp.net/ktegels/archive/2005/01/31/364272.aspx
@colegatron
colegatron / getviewport.php
Last active August 29, 2015 13:56
Google Maps Api Viewport : From a latitude and longitude and a radius get two latlong pairs to be used as a viewport (for example)
<?php
/**
* User: Ivan Gonzalez / Colegatron
* Date: 18/07/13
* Time: 16:14
*/
header('Content-Type: application/json');
$lat = $_GET["lat"];
@colegatron
colegatron / getviewport.php
Created July 18, 2013 14:52
From a latitude and longitude and a radius get two latlong pairs to be used as a viewport (for example)
<?php
/**
* User: Ivan Gonzalez / Colegatron
* Date: 18/07/13
* Time: 16:14
*/
header('Content-Type: application/json');
$lat = $_GET["lat"];