Skip to content

Instantly share code, notes, and snippets.

View doublenns's full-sized avatar

doublenns

  • Propense.ai
  • Dallas, TX
View GitHub Profile
@doublenns
doublenns / git_repo_statistics.sh
Last active February 12, 2021 20:04
Git Repo Statistics
# Local branches merged, or not merged, into master
git branch --merged master
git branch --no-merged master
# Number of commits contributed per author
git shortlog -s -n
# Number of lines contributed per author
# Location within repo executed matters
git ls-files | while read f; do git blame -w -M -C -C --line-porcelain "$f" | grep -I '^author '; done | sort -f | uniq -ic | sort -n --reverse
@doublenns
doublenns / check_connection_to_remote_port.py
Last active October 20, 2019 18:04
Check to see whether a port on a remote server is accessible.
import socket
def check_conn_to_remote_port(address, port):
s = socket.socket()
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
print(f"Attempting to connect to {address} on port {port}")
try:
s.connect((address, port))
print(f"Connected to {address} on port {port} -- success!")
@doublenns
doublenns / get_instance_region.py
Last active October 18, 2019 06:15
Retrieve EC2's region from instance metadata using Python
import requests
import sys
from requests.packages.urllib3 import Retry
def get_instance_region():
instance_identity_url = "http://169.254.169.254/latest/dynamic/instance-identity/document"
session = requests.Session()
retries = Retry(total=3, backoff_factor=0.3)
metadata_adapter = requests.adapters.HTTPAdapter(max_retries=retries)
session.mount("http://169.254.169.254/", metadata_adapter)
@doublenns
doublenns / get_instance_region.md
Last active December 15, 2021 11:13
Get EC2's region from instance metadata using Python

The region in which an EC2 instance is located is exposed via EC2 Instance Metadata.

Part of instance metadata is Instance Identity Documents. Specifically, the instance-identity/document endpoint is a dynamic data category that contains instance attributes such as the instanceId, privateIp, instanceType. And most relevantly the instance's region.

We can query the instance identity document via the 169.254.169.254 link-local address. (This only works on EC2 instances):

[ec2-user ~]$ curl http://169.254.169.254/latest/dynamic/instance-identity/document
{
  "devpayProductCodes" : null,
  "marketplaceProductCodes" : null,
@doublenns
doublenns / ip_of_docker_container.md
Last active October 18, 2019 06:08
Get Internal IP of Docker container

Get IP Address of specific Container:

CN="<docker_container_name>"
CIP=$(sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' $CN)

Retrieve all container IDs and corresponding IP addresses:

sudo docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' $(sudo docker ps -aq)
@doublenns
doublenns / yum_vars.txt
Created June 20, 2018 15:42
View Yum variables in STDOUT
python -c 'import yum, pprint; yb = yum.YumBase(); pprint.pprint(yb.conf.yumvar, width=1)'
Resource:
https://unix.stackexchange.com/a/20226
@doublenns
doublenns / network_port_troubleshooting.txt
Last active April 11, 2018 14:53
Troubleshooting network connectivity to a port when your Linux machine doesn't have Telnet or Netcat installed
# Check for network port connectivity on remote sys:
TCP_PORT_STATUS=`(echo > /dev/tcp/xx.xx.xx.xx/yyy) >/dev/null 2>&1 && echo "UP" || echo "DOWN"`; echo $TCP_PORT_STATUS
# xx.xx.xx.xx = Destination IP Address
# yyy = Destination Port
Ref: https://superuser.com/a/806331
# Create port listener on local computer using python
@doublenns
doublenns / CountOpenSafariTabs.scpt
Last active May 24, 2020 17:09 — forked from edenwaith/CountOpenSafariTabs.scpt
AppleScript: Count the number of open windows and tabs in Safari
#!/usr/bin/env osascript
-- Original Author: Chad Armstrong
-- Source: https://gist.github.com/edenwaith/2213a764ccb091d6a03989f238efb63f
-- Description: Count the number of open windows and tabs in Safari
tell application "Safari"
--Variables
set windowCount to count of windows
@doublenns
doublenns / run_shell_cmd.py
Last active November 21, 2019 19:43
Quick, dirty function to call shell commands from Python
#!/usr/bin/env python3
import subprocess
def run_shell_cmd(cmd, *shell):
# Might want to use a "try" or call.check in case Command fails
if "shell" or "sh" in shell.lower():
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE, shell=True)
@doublenns
doublenns / get_ou.ps1
Created August 30, 2017 16:49
Retrieve Active Directory Organizational Unit for machine and only display if matches w/ specified parameter
param([string]$env="DEV")
$computers = Get-Content Documents\servers.txt
Foreach ($server in $computers){
$ou_result = ([adsisearcher]"(&(name=$server)(objectClass=computer))").findall().path
echo $ou_result | Select-String -Pattern $env -NotMatch
}