Skip to content

Instantly share code, notes, and snippets.

@ryran
ryran / mll is for multipath listing
Last active December 19, 2015 15:19
A little bash function for parsing through multipath -ll output.
#!/bin/bash
function mll {
local inputfile search_cmd
# If directory, assume sosreport and look for multipath output
if [[ -d $1 ]]; then
inputfile=$1/sos_commands/devicemapper/multipath_-v4_-ll
[[ -r $inputfile ]] || { echo -e "'$inputfile' unreadable\nRun with no args to see usage"; return 2; }
elif [[ -f $1 && -r $1 ]]; then
inputfile=$1
@ryran
ryran / L is for loop
Last active December 19, 2015 15:19
A simple bash function for retrying commands until they succeed.
#!/bin/bash
# 'Loop' command
# I use this mostly for when machines are booting up and I don't want to
# keep manually trying to ssh over to them
function L {
local interval
if [[ $# -eq 0 || $1 = --help || $1 = -h || $1 = -\? ]]; then
echo "Usage: L [-N] COMMAND"
echo "Runs COMMAND [waiting N sec between failed attempts] until it succeeds"
@ryran
ryran / ddinfo
Last active December 19, 2015 15:19
A tiny bash function that causes dd to show status updates until it finishes.
#!/bin/bash
# Takes the pid of a dd process as an argument and loop-updates the status until it finishes
function ddinfo {
kill -USR1 $1
while :; do
kill -0 $1 && { sleep 1m; kill -USR1 $1; } || { echo "DONE"; break; }
done
}
@ryran
ryran / myips
Created July 11, 2013 13:52
Another tiny bash function for quickly showing local system IP addrs.
#!/bin/bash
# Simply prints non-127 ip addresses
function myips {
echo "$(hostname) IP addrs:" >&2
ip a | awk '/inet / {if ($2 !~ /^127/) print " "$2}'
}
@ryran
ryran / Distro is for distro-checking in sosreports
Last active December 19, 2015 15:49
A lil distro-checking function for when dealing with sosreports -- mainly this is for looking for non-rhel packages.
#!/bin/bash
# Run with the first argument as the path to the root of an extracted sosreport
function Distro {
ls -l "$1"/etc/*-release; echo
cat "$1"/etc/redhat-release; echo
grep --color=no ^serverURL= "$1"/etc/sysconfig/rhn/up2date; echo
egrep -i 'fedora|centos|oracle|enterprise|uek|suse|nv[0-9]' "$1"/installed-rpms
}
@ryran
ryran / memsum is for process inspection
Last active December 19, 2015 15:49
Bash function for making it quick & easy to inspect mem-usage of processes.
#!/bin/bash
# Uses pgrep, ps, and awk to sum the RSS & VSZ of any process names or PIDs passed to it
function memsum {
if [[ -z $1 ]]; then
echo "memsum: requires at least one PID or [partial] process name"
return 1
fi
local pid p
until [[ -z $1 ]]; do
if [[ $1 =~ ^[0-9]+$ ]]; then
@ryran
ryran / ssh_agent_systemwide is for making ssh-agent even cooler
Created July 18, 2013 14:13
An exciting bash function for extending the default capability of ssh-agent
#!/bin/bash
# Currently, I only have used this on RH/Fedora-based systems, so....
# The way *I* use this: I put the function into /etc/profile.d/reusessh.sh
# or something like that, and then call it from each user's ~/.bash_profile
# Reuse SSH Agent -- handles a per-user system-wide reusable ssh-agent
# This just sets up a function; you still need to call it from ~/.bash_profile
# (or simply run on demand, as a replacement for running 'ssh-agent bash')
function ssh_agent_systemwide
@ryran
ryran / atime_change_1month_ago is for making timestamps lie
Created July 18, 2013 14:16
A bash function for making it easy to change the access time of a file
#!/bin/bash
# (To change mtime instead of atime, switch the -a to -m)
atime_change_1month_ago()
{
if [[ -e $1 ]]; then
month=`date +%m`
[ `echo $month|cut -c1` -eq 0 ] && month=`echo $month|cut -c2`
month=$((month - 1))
[ `expr length $month` -eq 1 ] && month="0$month"
@ryran
ryran / zentest is for bash GUI
Created July 18, 2013 14:25
A simple example of using the coprocessor feature of bash -- in this case, to launch a GUI progress bar via zenity
#!/bin/bash
# Start Zenity (might not be installed by default) as a bash coprocess
coproc zenity --progress --pulsate --title='Testing' --text='Starting up...'
z1=${COPROC[1]}
# Update Zenity...
sleep 2s
echo 33 >&$z1
echo '#One third done...' >&$z1
@ryran
ryran / create-a-socket.c
Created March 3, 2016 22:45
tiny C application to create a unix socket (UDS) file
#include <fcntl.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char **argv)
{
// The following line expects the socket path to be first argument