Skip to content

Instantly share code, notes, and snippets.

View codejake's full-sized avatar

Jake Shaw codejake

View GitHub Profile
@codejake
codejake / ps-sed.txt
Last active December 11, 2018 16:38
sed-like fun with PowerShell.
PS1> Get-Content foo.txt
dog
cat
bird
fish
chicken
duck
giraffe
PS1> Get-Content foo.txt | %{$_ -replace "duck", "fruit"}
@codejake
codejake / gist:74208d678cbada093af21a6ac8f1b5da
Last active December 11, 2018 17:50
powershell-tokenizing fun
PS> cat .\pets.txt
dog,bowser,5
cat,mimi,4
snake,mr slithers,18
PS> get-content "pets.txt" | foreach-object {
$data = $_ -split ","
"{1} is a {0} who is {2} years old" -f $data[0],$data[1],$data[2]
}
@codejake
codejake / furry.py
Last active January 14, 2019 21:43
$ python
Python 3.7.2 (default, Dec 27 2018, 07:35:06)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def hello(fruit_basket):
... for i in fruit_basket:
... print(f"-- {i}")
...
>>> stuff = ( "apple", "carrot", "hamster")
@codejake
codejake / bash-variable-substitution-fun.bash
Last active October 9, 2019 15:44
Bash variable substitution fun
# These
$ FOO="abc123"
$ echo $FOO
abc123
# Get the variable length in chars: ${#FOO}
$ echo ${#FOO}
6
@codejake
codejake / try-catch.bash
Last active January 30, 2019 22:21
bash try/catch equivalents
# 1.) There is not try/catch in bash, but you can use the || and && operands as semi-equivalents.
# Think of || as "or".
# If command1 fails, then run command2:
$ command1 || command2
# Think of && as "and".
# If command1 succeeds, then run command2:
$ command1 && command2
@codejake
codejake / Rudimentary-logging.bash
Last active January 31, 2019 16:37
Bash rudimentary logging example
#!/bin/bash
export BU_DEST=${HOME}/data
export BU_DEST_USER=${BU_DEST}/${USER}
export LOGFILE=BU_DEST_USER/test.log
# If desired, clear the log file with >.
echo "" > ${LOGFILE}
# Append a log entry with the date/time to our log with >>
function prompt {
$currentUser = $env:USERNAME.ToLower()
$currentDirectory = get-location
$computerName = $env:COMPUTERNAME.ToLower()
Write-Host ""
Write-Host "[" -NoNewLine
Write-Host "${currentDirectory}" -ForegroundColor Yellow -NoNewLine
Write-Host "] on " -NoNewLine
Write-Host "${computerName} " -ForegroundColor Cyan -NoNewLine
#!/bin/bash
# unshare-all.sh
# Unshare all the printers on a macOS system.
# Jake Shaw
#
# version: 19.10.8.1
# One-liner version
# lpstat -p | awk '{ print $2 }' | while read printer; do lpadmin -p "$printer" -o printer-is-shared=false;done
! Create an Ether-channel
interface Port-Channel 1
switchport trunk encapsulation dot1q
switchport mode trunk
logging event bundle-status
logging event trunk-status
! Configure and add a port to the Ether-channel
int Gi0/13
@codejake
codejake / hook.ps1
Created August 10, 2020 17:26
Example: Send Slack Webhooks with PowerShell, without the need for some dumb library.
# We don't need no steenkin' PS libraries to Slack!
$results = Get-DhcpServerv4ScopeStatistics -ComputerName my_dhcp_server_fqdn | where {$_.PercentageInUse -gt 70} | select -property scopeid,percentageinuse
ForEach ($subnet in $results) {
$payload = @{
text="Warning: DHCP subnet " + $subnet.ScopeId + " is " + [math]::Round($subnet.percentageinuse) +"% full."
}
$json = $payload | ConvertTo-Json
$response = Invoke-RestMethod 'https://hooks.slack.com/services/T0AXXXXX/B01XXXXXXKF/uklXXXXXYYYYsb' -Method Post -Body $json -ContentType 'application/json'