Skip to content

Instantly share code, notes, and snippets.

@isaaclw
isaaclw / rescan.sh
Last active January 3, 2022 16:53
Force device rescan (see detect_sata.sh)
#!/bin/bash
set -e
# Note: 2021-09-09:
# I ran this script and scanned only the hosts that weren't listing a device
# However, it didn't find the block that I needed it to, until I did a global scan:
# ls /sys/class/scsi_host/ | while read host ; do echo "- - -" > /sys/class/scsi_host/$host/scan ; done
# I know this feels dangerous, I would avoid doing it if I understood what was going on, but I eventually just gave up.
# I think there's something wrong below with the matching of the `readlink /sys/block`
# After the full scan, two drives had the same 'host', so I'm not sure if that was the issue or what.
if (typeof String.prototype.startsWith != 'function') {
"use strict";
String.prototype.startsWith = function (str){
return this.slice(0, str.length) == str;
};
}
var time_wait = 0,
string_check = '{I want to {',
action = 'decline',
@isaaclw
isaaclw / reload_icinga.sh
Created November 21, 2019 20:33
Check for errors and then reload icinga
/usr/sbin/icinga -v /etc/icinga/icinga.cfg | grep -B 1 -iE "(warn|error|read)"; read -p "ready to reload? (ctrl+c to cancel)" foo; /etc/init.d/icinga reload
@isaaclw
isaaclw / copy_block_device.sh
Last active November 22, 2019 20:56
Copy Differences in block device #perl #hardware #blockdevice
#!/bin/bash
dev1=$1
remote=$(echo $2 | cut -d ':' -f 1)
dev2=$(echo $2 | cut -d ':' -f 2)
ssh -i ~/.ssh/id_rsa $remote "
perl -'MDigest::MD5 md5' -ne 'BEGIN{\$/=\1024};print md5(\$_)' $dev2 | lzop -c" |
lzop -dc | perl -'MDigest::MD5 md5' -ne 'BEGIN{$/=\1024};$b=md5($_);
read STDIN,$a,16;if ($a eq $b) {print "s"} else {print "c" . $_}' $dev1 | lzop -c |
ssh -i ~/.ssh/id_rsa $remote "lzop -dc |
@isaaclw
isaaclw / detect_sata.sh
Last active November 22, 2019 21:09
Detect Sata (may not work) #hardware #blockdevices #bash
# first list devices that are connected:
ls /dev/sd*
# then list devices listed in /sys/block:
ls /sys/block/sd* # I expect they agree, but unsure atm
# find the 'hostid' of the existing devices
for i in $(ls /dev/); do if [[ "$i" =~ "sd" ]]; then (echo -n "$i => "; readlink /sys/block/$i | cut -d '/' -f 5; echo "") | grep "host"; fi; done
@isaaclw
isaaclw / lvs_verbose.sh
Created November 21, 2019 20:38
Command to see all lvm segments
sudo lvs --segments -o +pe_ranges
@isaaclw
isaaclw / psql_snap.sh
Last active January 3, 2022 16:00
Use LVM Snapshots to rollback Postgresq
#!/bin/bash
# This was to create a quick snapshot of the database, make the changes I
# wanted, and then revert the database to exactly the way it was before I
# snapshotted it.
LV=pg_ot
VG=$(/sbin/vgdisplay -s | sed -e 's/ "\([^"]*\)" .*/\1/')
MTPT=/var/lib/postgresql/11/main
PROC=postgres
VERSION=11
@isaaclw
isaaclw / confirm.sh
Created November 21, 2019 20:42
Confirm a command
#!/bin/bash
echo -n "Do you want to run '$*'? [N/y] "
read -N 1 REPLY
echo
if test "$REPLY" = "y" -o "$REPLY" = "Y"; then
"$@"
else
echo "Cancelled by user"
fi
@isaaclw
isaaclw / load_display.sh
Last active November 27, 2022 15:12
Load display
# http://stackoverflow.com/a/1162345/971529
load_display() {
if [ -z "$1" ]; then
PID=$(pgrep -n -u $USER 'gnome-session|xfdesktop|xterm|kodi|lxsession|mate-session')
else
PID=$(pgrep -n -u $USER -f "$1")
fi
FALLBACK=$(pgrep -n 'unity-greeter')
if [ -n "$PID" ]; then
export DISPLAY=$(cat /proc/$PID/environ | strings | awk 'BEGIN{FS="=";} $1=="DISPLAY" {print $2; exit}')
@isaaclw
isaaclw / load_ssh_key.sh
Last active November 27, 2022 21:38
Load the ssh key #function #bash #ssh
load_ssh_key() {
key=~/.ssh/id_ed25519
force=0
if [ -n "$1" ]; then
if [ -e "$1" ]; then
key=$(readlink -f $1)
elif [ "$1" == "reset-all" ]; then
echo "resetting"
pkill --signal 9 -f ssh-agent
force=1