Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- tab-width: 4 -*- ;; Emacs
- # vi: set tabstop=8 :: Vi/ViM
- #
- # .tcshrc
- # Author: Devin Teske <dteske@freebsd.org>
- # Last Change: 2022 June 9
- ############################################################ CONFIGURATION
- #
- # ka (keep-alive) command fortune(6) file
- #
- setenv KA_FORTUNE freebsd-tips
- ############################################################ GLOBALS
- #
- # Global exit status variables
- #
- setenv SUCCESS 0
- setenv FAILURE 1
- #
- # Are we running interactively?
- #
- set interactive = 0
- if ( $?loginsh ) set interactive = 1
- #
- # Key bindings
- #
- if ( "$interactive" ) then
- bindkey "^W" backward-delete-word
- bindkey "^U" backward-kill-line
- endif
- #
- # Add ~/bin to PATH
- #
- switch ( $PATH )
- case ~/bin:
- case ~/bin\:*:
- case *\:~/bin\:*:
- case *\:~/bin:
- case $HOME/bin:
- case $HOME/bin\:*:
- case *\:$HOME/bin\:*:
- case *\:$HOME/bin:
- case ~/bin/:
- case ~/bin/\:*:
- case *\:~/bin/\:*:
- case *\:~/bin/:
- case $HOME/bin/:
- case $HOME/bin/\:*:
- case *\:$HOME/bin/\:*:
- case *\:$HOME/bin/:
- breaksw
- default:
- if ( $?PATH ) setenv PATH "$PATH:"
- setenv PATH "$PATH$HOME/bin"
- endsw
- #
- # OS Specifics
- # NB: Requires uname(1) -- from base system
- #
- setenv UNAME_S `uname -s`
- setenv UNAME_R `uname -r`
- setenv UNAME_P `uname -p`
- setenv MESSAGES /var/log/messages
- if ( ! $?UNAME_s ) setenv UNAME_s $UNAME_S
- #
- # Where are apache's log files?
- #
- switch ( $UNAME_S )
- case FreeBSD:
- switch ( $UNAME_R )
- case 4.8-*:
- setenv APACHE_ERROR_LOG /usr/local/www/logs/error_log
- setenv APACHE_ACCESS_LOG /usr/local/www/logs/access_log
- breaksw
- default:
- setenv APACHE_ERROR_LOG /usr/local/apache2/logs/error_log
- setenv APACHE_ACCESS_LOG /usr/local/apache2/logs/access_log
- endsw
- breaksw
- case Linux:
- setenv APACHE_ERROR_LOG /etc/httpd/logs/error_log
- setenv APACHE_ACCESS_LOG /etc/httpd/logs/access_log
- breaksw
- case CYGWIN*:
- setenv APACHE_ERROR_LOG /var/log/apache2/error_log
- setenv APACHE_ACCESS_LOG /var/log/apache2/access_log
- breaksw
- endsw
- #
- # Set DISPLAY for Xming
- #
- switch ( $UNAME_S )
- case CYGWIN*:
- setenv DISPLAY localhost:0
- endsw
- #
- # For dialog(1) and Xdialog(1) menus -- mainly ssh-agent-dup in FUNCTIONS below
- #
- set DIALOG_MENU_TAGS = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- #
- # Default directory to store dialog(1) and Xdialog(1) temporary files
- #
- if ( ! $?DIALOG_TMPDIR ) set DIALOG_TMPDIR = "/tmp"
- #
- # Literals
- # NB: Required by escape, shfunction, and eshfunction
- #
- set asc = `printf '\033['`
- set tab = " " # Must be a literal tab
- set nl = "\
- " # END-QUOTE
- ############################################################ ALIASES
- unalias quietly >& /dev/null
- alias quietly '\!* >& /dev/null'
- quietly unalias grep
- alias grep "grep --color"
- quietly unalias have
- alias have 'which \!* >& /dev/null'
- quietly unalias eval2
- alias eval2 'echo \!*; eval \!*'
- alias csh_na 'echo "Not available for [t]csh"; false'
- alias path_munge csh_na
- # settitle $title ...
- #
- # Set the title of your ANSI-compatible shell window.
- #
- alias settitle printf '"\033]2;\!*\a\033]1;\!*\a\033]0;\!*\a"'
- quietly unalias escape
- alias escape "awk '"' \\
- BEGIN { a = sprintf("%c",39) } \\
- { \\
- gsub(a,"&\\\\&&") \\
- gsub(/ /, a "\\ " a) \\
- gsub(/\t/, a "$tab:q" a) \\
- buf=buf a "$nl:q" a $0 \\
- } \\
- END { print a substr(buf,8) a } \\
- '\'
- # ssh-agent [ssh-agent options]
- #
- # Override ``ssh-agent'' to call an alias (you can always call the real binary
- # by executing /usr/bin/ssh-agent) that launches a background ssh-agent that
- # times-out in 30 minutes.
- #
- # Will evaluate the output of /usr/bin/ssh-agent (the real ssh-agent) called
- # with either a known-secure set of arguments (if none are provided) or the
- # unmodified arguments to this alias.
- #
- # Purpose is to prevent memorizing something like ``eval "`ssh-agent ...`"''
- # but instead simply ``ssh-agent [...]''.
- #
- # This allows you to, for example:
- #
- # ssh-agent
- # : do some ssh-add
- # : do some commits
- # ssh-agent -k
- # : or instead of ``ssh-agent -k'' just wait 30m for it to die
- #
- # NB: Requires ssh-agent -- from base system
- #
- quietly unalias ssh-agent
- alias ssh-agent 'eval `/usr/bin/ssh-agent -c -t 1800 \!*`'
- # function $name $code
- #
- # Define a ``function'' that runs in the current namespace.
- #
- # NB: Evaluated context (using `eval') may not merge namespace immediately.
- # NB: Commands considered unsafe by the shell may not work in this context.
- # NB: Using builtins such as if, while, switch, and others will invoke an adhoc
- # parser that appends to the history.
- #
- set alias_function = ' \
- set __name = $argv_function[1] \
- set __body = $argv_function[2]:q \
- set __var = "$__name:as/-/_/" \
- set __argv = argv_$__var \
- set __alias = alias_$__var \
- set __body = "set ALIASNAME = $__name; "$__body:q \
- alias $__name "set $__argv = (\!"\*");"$__body:q \
- unset $__alias \
- set $__alias = $__body:q \
- '
- quietly unalias function
- alias function "set argv_function = (\!*); "$alias_function:q
- # zless [OPTIONS] $file ...
- #
- # Allow quick paging of gzip-compressed files. Any options passed in-addition
- # to the filename pertain to zcat(1).
- #
- # NOTE: Some systems provide zless as an actual utility. For those systems,
- # we'll fallback to the actual utility opposed to this alias.
- #
- have zless || alias zless 'zcat \!* | less'
- alias tlog csh_na
- alias cvspicker csh_na
- # hgrep ...
- #
- # Use in-place of grep(1) whenever you want to preserve the first line of
- # output (often the header when viewing output from ps(1), lsof(8), df(1),
- # netstat(8), arp(8), lsmod(8), route(8), free(1), and many many others).
- #
- alias hgrep "/bin/sh -c '"' \\
- timeout=0; \\
- [ "$BASH_VERSION" ] && timeout=1 && shift; \\
- if [ $# -gt 1 ]; then \\
- for arg in "$@"; do \\
- [ -f "$arg" ] && head -1 "$arg" && break; \\
- done; \\
- else \\
- ( \\
- IFS=; \\
- read -r -t $timeout LINE; \\
- printf "\\e[1m%s\\e[0m\\n" "$LINE" \\
- ); \\
- fi; \\
- grep "$@" \\
- '"' -- /bin/sh"
- alias myshopts_usage csh_na
- alias myshopts csh_na
- alias cvs_admin_binaries csh_na
- alias cvs_admin_binaries_undo csh_na
- alias cvs_add_recursive csh_na
- #
- # zlog_wrapper aliases (see zlog_wrapper above)
- #
- # !!!the below usage statement is generated from zlog_usage below!!!
- # Usage: ALIAS [OPTIONS] [PATTERN]
- # Arguments:
- # PATTERN Awk(1) regular expression pattern to search logfile for.
- # Options:
- # -h Print this usage statement and exit.
- # -n Only consider non-matching lines.
- # -d1,2,3 Comma-separated list of field numbers to display as the
- # date/time when providing summary output (ignored if `-v'
- # is passed). Default is 1,2,3. A value of `NF' prints the
- # entire line.
- # -s Summary. Show summarization of lines.
- # -v Verbose. Show individual lines.
- #
- alias messages 'zlog_wrapper messages "$MESSAGES"'
- alias aelog 'zlog_wrapper aelog "$APACHE_ERROR_LOG" -d1,2,3,4,5'
- alias aalog 'zlog_wrapper aalog "$APACHE_ACCESS_LOG" -d4,5,1'
- #
- # tlog aliases (see tlog above)
- #
- # Usage: ALIAS [OPTIONS]
- # Options are sent to tail(1).
- #
- alias tm 'tlog "$MESSAGES"'
- alias tael 'tlog "$APACHE_ERROR_LOG"'
- alias taal 'tlog "$APACHE_ACCESS_LOG"'
- #
- # Random keepalive [ka] alias (based on system-availability of various
- # fun utilities).
- #
- alias RANDOM 'printf "%d\n" 0`od</dev/urandom|awk '\''{print $NF;exit}'\''`'
- @ n = 0
- have worms && eval set ka$n='"worms -d 25"' && @ n++
- have rain && eval set ka$n='"rain -d 50"' && @ n++
- have grdc && eval set ka$n='"grdc"' && @ n++
- have fortune
- if ( $? == 0 ) then
- set fort='sh -c "while :;do fortune $KA_FORTUNE;sleep 80;echo --;done"'
- eval set ka$n="'$fort'"
- @ n++
- endif
- if ( $n <= 0 ) then
- set ka0="sh -c 'while :;do echo -n .;sleep 80;done'"
- @ n++
- endif
- alias ka '@ x = `RANDOM` / ( 65537 / '$n' ); eval eval \$ka$x'
- unset n
- alias dbcat csh_na
- #
- # Alias for becoming root while maintaining shell customizations (in reverse-
- # order of preference)
- #
- have su && alias srsu 'su -m root'
- have sudo && alias srsu 'sudo env tcsh'
- have sr && alias srsu 'sr env tcsh'
- #
- # Editor settings (in reverse-order of preference)
- #
- have emacs && setenv EDITOR emacs
- have pico && setenv EDITOR pico
- have nano && setenv EDITOR nano
- have ee && setenv EDITOR ee
- have vi && setenv EDITOR vi
- have vim
- if ( ! $status ) then
- setenv EDITOR vim
- alias vi vim
- alias dview "vim +/'^\(Index\|diff\|--\|@@\|====\).*' +'set hls' +'set syntax=diff' -R"
- endif
- alias edit $EDITOR
- have vimdiff
- if ( $status ) then
- alias vimdiff "vim --cmd 'set scrollbind nowrap foldmethod=diff foldcolumn=2 diff' -O"
- endif
- #
- # Forthisms
- #
- # I was spending a lot of time in Forth while writing loader_menu for FreeBSD,
- # and couldn't stop myself from using these to no-end of pain. So I decided to
- # add the below to ease my pain.
- #
- alias .s 'ls'
- #
- # View man(1) pages with vim(1)
- #
- have vim && alias man 'man -P "col -b | vim -c '"'set ft=man nomod nolist'"' -"'
- #
- # Abbreviations
- #
- alias b basename
- alias d date
- alias di 'dirs -v'
- alias h history
- alias j jobs
- alias la 'ls -al'
- alias ll 'ls -l'
- alias lt 'ls -Altr'
- alias pd pushd
- alias po popd
- alias z suspend
- alias bx BitchX
- #
- # OS-specific Aliases (until they can be generalized)
- #
- switch ( $UNAME_S )
- case Linux:
- #
- # Make ls(1) colorful
- #
- alias ls='ls --color=tty'
- #
- # X-related aliases
- #
- alias Xsession /etc/X11/xdm/Xsession
- alias xsession Xsession
- breaksw
- case FreeBSD:
- case Darwin:
- #
- # boottime: Prints the date/time (format based on current locale)
- # that the system booted (resolution is 1-second).
- # bootsec: Prints the number of seconds since the epoch (see `%s'
- # format in date(1)) that was recorded during boot,
- # allowing you to use the `-r seconds' syntax of date(1)
- # to represent the boottime in any desired format.
- #
- set kbt="sysctl kern.boottime"
- alias boottime "$kbt | sed -e 's/.*} //'"
- alias bootsec "$kbt | sed -e 's/.* sec = \([0-9]\{1,\}\).*/\1/'"
- unset kbt
- #
- # bootfile: Prints the path to the running kernel.
- #
- alias bootfile 'sysctl -n kern.bootfile'
- #
- # Make ls(1) colorful
- #
- alias ls 'ls -AG'
- #
- # X-related aliases
- #
- alias Xsession /usr/X11R6/lib/X11/xdm/Xsession
- alias xsession Xsession
- breaksw
- endsw
- ############################################################ FUNCTIONS
- # cmdsubst $var [$env ...] $cmd
- #
- # Evaluate $cmd via /bin/sh and store the results in $var.
- # Like "set $var = `env $env /bin/sh -c $cmd:q`" except output is preserved.
- #
- # NB: This function is unused in this file
- # NB: Requires escape alias -- from this file
- # NB: Requires /bin/sh -- from base system
- #
- quietly unalias cmdsubst
- function cmdsubst ' \
- set __var = $argv_cmdsubst[1] \
- set __argc = $#argv_cmdsubst \
- @ __argc-- \
- set __penv = ($argv_cmdsubst[2-$__argc]:q) \
- @ __argc++ \
- set __cmd = $argv_cmdsubst[$__argc]:q \
- set __out = `env $__penv:q /bin/sh -c $__cmd:q | escape` \
- eval set $__var = $__out:q \
- '
- # evalsubst [$env ...] $cmd
- #
- # Execute $cmd via /bin/sh and evaluate the results.
- # Like "eval `env $env /bin/sh -c $cmd:q`" except output is preserved.
- #
- # NB: This function is unused in this file
- # NB: Requires escape alias -- from this file
- # NB: Requires /bin/sh -- from base system
- #
- quietly unalias evalsubst
- function evalsubst ' \
- set __argc = $#argv_evalsubst \
- @ __argc-- \
- set __penv = ($argv_evalsubst[1-$__argc]) \
- @ __argc++ \
- eval eval `env $__penv:q /bin/sh -c \\
- $argv_evalsubst[$__argc]:q | escape` \
- '
- # shfunction $name $code
- #
- # Define a ``function'' that runs under /bin/sh.
- # NB: No alias is created if one already exists.
- #
- quietly unalias shfunction
- function shfunction ' \
- set __name = $argv_shfunction[1] \
- set __argc = $#argv_shfunction \
- @ __argc-- \
- set __penv = ($argv_shfunction[2-$__argc]:q) \
- @ __argc++ \
- set __body = $argv_shfunction[$__argc]:q \
- set __var = "$__name:as/-/_/" \
- set __alias = shalias_$__var \
- set __func = shfunc_$__var \
- set __interp = "env $__penv:q /bin/sh -c "\"\$"${__alias}:q"\" \
- set __body = "local FUNCNAME=$__var ALIASNAME=$__name; $__body:q" \
- set __body = "$__var(){$nl:q$__body:q$nl:q}" \
- set $__func = $__body:q \
- set $__alias = $__body:q\;\ $__var\ \"\$@\" \
- have $__name || alias $__name "$__interp /bin/sh" \
- '
- # eshfunction $name $code
- #
- # Define a ``function'' that runs under /bin/sh but produces output that is
- # evaluated in the current shell's namespace.
- #
- # NB: No alias is created if one already exists.
- #
- quietly unalias eshfunction
- function eshfunction ' \
- set __name = $argv_eshfunction[1] \
- set __argc = $#argv_eshfunction \
- @ __argc-- \
- set __penv = ($argv_eshfunction[2-$__argc]:q) \
- @ __argc++ \
- set __body = $argv_eshfunction[$__argc]:q \
- set __var = "$__name:as/-/_/" \
- set __alias = shalias_$__var \
- set __func = shfunc_$__var \
- set __interp = "env $__penv:q /bin/sh -c "\"\$"${__alias}:q"\" \
- set __interp = "$__interp:q /bin/sh \!"\*" | escape" \
- set __body = "local FUNCNAME=$__var ALIASNAME=$__name; $__body:q" \
- set __body = "$__var(){$nl:q$__body:q$nl:q}" \
- set $__func = $__body:q \
- set $__alias = $__body:q\;\ $__var\ \"\$@\" \
- have $__name || alias $__name \\
- '\''eval eval `'\''$__interp:q'\''`'\'' \
- '
- # quietly $cmd ...
- #
- # Execute /bin/sh $cmd while sending stdout and stderr to /dev/null.
- #
- shfunction quietly '"$@" > /dev/null 2>&1'
- # have name
- #
- # Silently test for name as an available command, builtin, or other executable.
- #
- shfunction have 'type "$@" > /dev/null 2>&1'
- # eval2 $cmd ...
- #
- # Print $cmd on stdout before executing it.
- #
- shfunction eval2 'echo "$*"; eval "$@"'
- # fprintf $fd $fmt [$opts ...]
- #
- # Like printf, except allows you to print to a specific file-descriptor. Useful
- # for printing to stderr (fd=2) or some other known file-descriptor.
- #
- quietly unalias fprintf
- shfunction fprintf ' \
- fd=$1 \
- [ $# -gt 1 ] || return ${FAILURE:-1} \
- shift 1 \
- printf "$@" >&$fd \
- '
- # eprintf $fmt [$opts ...]
- #
- # Like printf, except send output to stderr (fd=2).
- #
- quietly unalias eprintf
- shfunction eprintf \
- '__fprintf=$shfunc_fprintf:q' \
- ' \
- eval "$__fprintf" \
- fprintf 2 "$@" \
- '
- # zlog $logfile [-n] [$pattern]
- #
- # A general purpose log searching function. Given $log base pathname, this
- # function will search for all occurrences of $pattern in $logfile and
- # $logfile.N.{gz,bz2} [FreeBSD] or $logfile.N [Linux] (preserving reverse-
- # chronological order, searching oldest logfiles first).
- #
- # If $pattern is NULL or omitted, all lines are returned.
- # If -n is given, non-matching lines are returned.
- #
- # NOTE: $pattern is an awk regular expression.
- #
- # NB: Requires have() -- from this file
- #
- quietly unalias zlog
- shfunction zlog \
- '__have=$shfunc_have:q' \
- ' \
- eval "$__have" \
- \
- local use_stdin= \
- if [ -t 0 ]; then \
- local logfile="$1" \
- else \
- local logfile="/dev/stdin" \
- use_stdin=1 \
- fi \
- local logfiles="$logfile" zlogs="" n=0 \
- local nomatch= \
- local sudo= needsudo= \
- \
- shift 1 # logfile \
- \
- local OPTIND flag \
- while getopts n flag; do \
- case "$flag" in \
- n) nomatch=1 ;; \
- esac \
- done \
- shift $(( $OPTIND - 1 )) \
- \
- # \
- # Check for two conditions where we would need sudo \
- # 1. If the log file is not readable \
- # 2. If the parent directory is not readable/traversable \
- # \
- if [ ! "$use_stdin" ]; then \
- [ -r "$logfile" ] || needsudo=1 \
- case "$logfile" in \
- */*) [ -r "${logfile%/*}" ] || needsudo=1;; \
- esac \
- fi \
- \
- if [ "$needsudo" ]; then \
- if [ "$UNAME_s" = "Linux" -a "$Linux" = "Ubuntu" ]; then \
- sudo=sudo \
- else \
- if have sr; then \
- sudo=sr \
- elif have sudo; then \
- sudo=sudo \
- fi \
- fi \
- fi \
- \
- case "$UNAME_s" in \
- FreeBSD) \
- # \
- # FreeBSD rotates logs in the following fashion: \
- # \
- # FILENAME DESCRIPTION \
- # logfile current log file \
- # logfile.0.gz most-recently rotated log file \
- # logfile.1.gz second-most recently rotated log file \
- # \
- local n=0 \
- if [ "$needsudo" ]; then \
- while $sudo [ -f "$logfile.$n.gz" ]; do \
- zlogs="$logfile.$n.gz $zlogs" \
- n=$(( $n + 1 )) \
- done \
- if [ ! "$zlogs" ]; then \
- n=0 \
- while $sudo [ -f "$logfile.$n.bz2" ]; do \
- zlogs="$logfile.$n.bz2 $zlogs" \
- n=$(( $n + 1 )) \
- done \
- fi \
- elif [ ! "$use_stdin" ]; then \
- while [ -f "$logfile.$n.gz" ]; do \
- [ -r "$logfile.$n.gz" ] || needsudo=1 \
- zlogs="$logfile.$n.gz $zlogs" \
- n=$(( $n + 1 )) \
- done \
- if [ ! "$zlogs" ]; then \
- n=0 \
- while $sudo [ -f "$logfile.$n.bz2" ]; do \
- zlogs="$logfile.$n.bz2 $zlogs" \
- n=$(( $n + 1 )) \
- done \
- fi \
- fi \
- ;; \
- Linux) \
- # \
- # Linux rotates logs in the following fashion: \
- # \
- # FILENAME DESCRIPTION \
- # logfile current log file \
- # logfile.1 most-recently rotated log file \
- # logfile.2 second-most recently rotated log file \
- # \
- local n=1 \
- if [ "$needsudo" ]; then \
- while $sudo [ -f "$logfile.$n" ]; do \
- logfiles="$logfile.$n $logfiles" \
- n=$(( $n + 1 )) \
- done \
- elif [ ! "$use_stdin" ]; then \
- while [ -f "$logfile.$n" ]; do \
- [ -r "$logfile.$n" ] || needsudo=1 \
- logfiles="$logfile.$n $logfiles" \
- n=$(( $n + 1 )) \
- done \
- fi \
- ;; \
- esac \
- \
- if [ "$nomatch" ]; then \
- awk_code='\'' \\
- $0 ~ pattern { ign = 1; next } \\
- ign && /repeated/ { next } \\
- { ign = 0; print } \\
- '\'' # END-QUOTE \
- else \
- awk_code='\'' \\
- $0 ~ pattern { ok = 1; print; next } \\
- ok && /repeated/ { print; next } \\
- { ok = 0 } \\
- '\'' # END-QUOTE \
- fi \
- \
- local pattern="$1" \
- if [ "$zlogs" ]; then \
- $sudo zcat $zlogs | sh -c "cat; cat $logfiles" | \
- awk -v pattern="$pattern" "$awk_code" \
- else \
- $sudo cat $logfiles | \
- awk -v pattern="$pattern" "$awk_code" \
- fi \
- '
- # zlog_summary $logfile [-d1,2,3] [-n] [$pattern]
- #
- # A wrapper around the zlog general purpose log searching function designed to
- # provide a brief summary containing the following information:
- # - The first line matching $pattern
- # - The timestamp of the first occurrence
- # - The timestamp of the last occurrence
- # - How many occurrences were found
- # NOTE: Also counts "last message repeated N times" entries
- #
- # NOTE: If $pattern is NULL or omitted, the first/oldest log-entry is printed.
- # In addition, the timestamps in the summary will instead be the first
- # (oldest) and last (newest) entry, respectively. This serves as a sort
- # of brief look at the date-range spanning the individual log files.
- #
- # NOTE: $pattern is an awk regular expression.
- #
- # By default, it is assumed that the date/time fields to be displayed are the
- # first, second, and third fields of each log entry, separated by whitespace.
- # For example, /var/log/messages has the following first three fields:
- #
- # Jun 5 04:02:10
- #
- # However, not all logfiles display date/time entries in the above manner.
- # Therefore, the following options can be passed to customize which line-fields
- # (and how many) are displayed for the date/time:
- #
- # For example, the Apache error_log displays the date/time in five pieces:
- #
- # [Sun May 31 04:02:14 2009]
- #
- # So to display the proper values for date-range outputs when parsing the
- # Apache error_log, you should pass the following parameters:
- #
- # -d1,2,3,4,5
- #
- # You can optionally pass `-dNF' to print the whole line.
- #
- # If `-n' is given, only non-matching lines are considered.
- #
- # NB: Requires zlog() -- from this file
- # NB: Requires awk(1) -- from base system
- #
- quietly unalias zlog_summary
- shfunction zlog_summary \
- '__have=$shfunc_have:q' \
- '__zlog=$shfunc_zlog:q' \
- ' \
- eval "$__have" \
- eval "$__zlog" \
- \
- local datetime="1,2,3" \
- local logfile="$1" \
- local nomatch= \
- \
- shift 1 # logfile \
- \
- local OPTIND flag \
- while getopts d:n flag; do \
- case "$flag" in \
- d) datetime="$OPTARG";; \
- n) nomatch=1 ;; \
- esac \
- done \
- shift $(( $OPTIND - 1 )) \
- \
- zlog "$logfile" ${nomatch:+-n} "$1" | \
- awk -v datetime="$datetime" '\'' \\
- BEGIN { \\
- hr = sprintf("%-60s", "") \\
- gsub(" ", "-", hr) \\
- ndt = split(datetime, dt, /,/) \\
- } \\
- function dtparse( n, parsed) \\
- { \\
- if (length(datetime) <= 0) return "" \\
- parsed = "" \\
- for (n = 1; n <= ndt; n++) \\
- parsed = sprintf("%s %s", parsed, $dt[n]) \\
- return substr(parsed, 2) \\
- } \\
- function summarize() \\
- { \\
- count += /repeated/ ? $(NF-1) : 1 \\
- last = dtparse() \\
- } \\
- ! init { \\
- init = 1 \\
- print hr \\
- print $0 \\
- print hr \\
- print " 1st: " dtparse() \\
- } \\
- { summarize() } \\
- END { \\
- printf "Last: %s\n", last \\
- printf "Total Occurrences: %d\n", count \\
- print hr \\
- } \\
- '\'' # END-QUOTE \
- '
- # zlog_usage [$FUNCNAME]
- #
- # Print usage statement for zlog_wrapper family of functions.
- #
- # NB: Requires eprintf() -- from this file
- #
- quietly unalias zlog_usage
- shfunction zlog_usage \
- '__eprintf=$shfunc_eprintf:q' \
- '__fprintf=$shfunc_fprintf:q' \
- ' \
- eval "$__fprintf" \
- eval "$__eprintf" \
- \
- local func="${1:-$FUNCNAME}" \
- local optfmt='\''\t%-9s %s\n'\'' \
- eprintf "Usage: %s [-hnv] [-d1,2,3] [PATTERN]\n" "$func" \
- eprintf "Options:\n" \
- eprintf "$optfmt" "PATTERN" "Awk(1) regular expression pattern to search logfile for." \
- eprintf "$optfmt" "-d1,2,3" "Comma-separated list of field numbers to display as the" \
- eprintf "$optfmt" "" "date/time when providing summary output (ignored if \`-v'\''" \
- eprintf "$optfmt" "" "is passed). Default is 1,2,3. A value of \`NF'\'' prints the" \
- eprintf "$optfmt" "" "entire line." \
- eprintf "$optfmt" "-h" "Print this usage statement and exit." \
- eprintf "$optfmt" "-n" "Only consider non-matching lines." \
- eprintf "$optfmt" "-v" "Verbose. Show individual lines." \
- return ${FAILURE:-1} \
- '
- # zlog_wrapper $FUNCNAME $logfile [OPTIONS] [$pattern]
- #
- # A wrapper for the zlog/zlog_summary functions.
- #
- # NB: Requires zlog() zlog_summary() zlog_usage() -- from this file
- #
- quietly unalias zlog_wrapper
- shfunction zlog_wrapper \
- '__eprintf=$shfunc_eprintf:q' \
- '__fprintf=$shfunc_fprintf:q' \
- '__zlog=$shfunc_zlog:q' \
- '__zlog_summary=$shfunc_zlog_summary:q' \
- '__zlog_usage=$shfunc_zlog_usage:q' \
- ' \
- eval "$__eprintf" \
- eval "$__fprintf" \
- eval "$__zlog" \
- eval "$__zlog_summary" \
- eval "$__zlog_usage" \
- \
- local func="${1:-$FUNCNAME}" \
- local logfile="$2" \
- local datetime= \
- local verbose= \
- local nomatch= \
- \
- shift 2 # func/logfile \
- \
- [ -t 1 ] || verbose=1 # stdout is a pipe \
- \
- local OPTIND flag \
- while getopts d:hnsv flag; do \
- case "$flag" in \
- d) datetime="$OPTARG" ;; \
- n) nomatch=1 ;; \
- s) verbose= ;; \
- v) verbose=1 ;; \
- *) zlog_usage "$func" \
- return ${FAILURE:-1} \
- esac \
- done \
- shift $(( $OPTIND - 1 )) \
- \
- set -- ${nomatch:+-n} "$@" \
- cmd=zlog \
- if [ ! "$verbose" ]; then \
- cmd=zlog_summary \
- set -- ${datetime:+-d "$datetime"} "$@" \
- fi \
- $cmd "$logfile" "$@" \
- '
- # ssh-agent-dup [-aqn]
- #
- # Connect to an open/active ssh-agent session available to the currently
- # authenticated user. If more than one ssh-agent is available and the `-n' flag
- # is not given, provide a menu list of open/active sessions available. Allows
- # the user to quickly duplicate access to an ssh-agent launched in another
- # interactive session on the same machine or for switching between agents.
- #
- # This allows you to, for example:
- #
- # (in shell session A)
- # ssh-agent
- # (in shell session B)
- # ssh-agent-dup
- # (now both sessions A and B can use the same agent)
- #
- # No menu is presented if only a single agent session is available (the open
- # session is duplicated for the active shell session). If more than one agent
- # is available, a menu is presented. The menu choice becomes the active agent.
- #
- # If `-a' is present, list all readable agent sockets, not just those owned by
- # the currently logged-in user.
- #
- # If `-q' is present, do not list agent nor keys.
- #
- # If `-n' is present, run non-interactively (good for scripts; pedantic).
- #
- # NB: Requires cexport() dialog_menutag() dialog_menutag2help() have()
- # quietly() -- from this file
- # NB: Requires $DIALOG_TMPDIR $DIALOG_MENU_TAGS -- from this file
- # NB: Requires awk(1) cat(1) grep(1) id(1) ls(1) ps(1) ssh-add(1) stat(1)
- # -- from base system
- #
- shfunction cexport ' \
- local item key value \
- while [ $# -gt 0 ]; do \
- item="$1" \
- key="${item%%=*}" \
- value="${item#"$key"=}" \
- if [ "$interactive" ]; then \
- echo "setenv $key $value" >&2 \
- fi \
- echo "setenv $key $value" \
- export "$item" \
- shift 1 # item \
- done \
- '
- quietly unalias cexport # sh only
- quietly unalias ssh-agent-dup
- eshfunction ssh-agent-dup \
- 'DIALOG_MENU_TAGS=$DIALOG_MENU_TAGS:q' \
- 'DIALOG_TMPDIR=$DIALOG_TMPDIR:q' \
- '__cexport=$shfunc_cexport:q' \
- '__dialog_menutag=$shfunc_dialog_menutag:q' \
- '__dialog_menutag2help=$shfunc_dialog_menutag2help:q' \
- '__have=$shfunc_have:q' \
- '__quietly=$shfunc_quietly:q' \
- ' \
- eval "$__cexport" \
- eval "$__dialog_menutag" \
- eval "$__dialog_menutag2help" \
- eval "$__have" \
- eval "$__quietly" \
- \
- local nsockets=0 \
- local t=1s # ssh-add(1) timeout \
- local list_all= quiet= interactive=1 noninteractive= \
- local sockets= \
- local ucomm owner socket stat pid current_user \
- \
- local OPTIND=1 OPTARG flag \
- while getopts anq flag; do \
- case "$flag" in \
- a) list_all=1 ;; \
- n) noninteractive=1 interactive= ;; \
- q) quiet=1 ;; \
- \?|*) \
- [ "$noninteractive" ] || \
- echo "$ALIASNAME [-aq]" | ${LOLCAT:-cat} >&2 \
- return ${FAILURE:-1} \
- esac \
- done \
- shift $(( $OPTIND - 1 )) \
- \
- case "$UNAME_s" in \
- *BSD) stat="-f%Su" ;; \
- *) stat="-c%U" \
- esac \
- \
- current_user=$( id -nu ) \
- for socket in /tmp/ssh-*/agent.[0-9]*; do \
- # Must exist as a socket \
- [ -S "$socket" ] || continue \
- \
- if have lsof; then \
- pid=$( lsof -t -- "$socket" 2> /dev/null ) || \
- continue \
- else \
- # Must end in numbers-only (after trailing dot) \
- pid="${socket##*.}" \
- [ "$pid" -a "$pid" = "${pid#*[\!0-9]}" ] || \
- continue \
- \
- for pid in $(( $pid + 1 )) $pid $(( $pid + 2 )) "" \
- do \
- [ "$pid" ] || break \
- # Potential PIDs based on socket num \
- # Must be running pid and ssh[d]/agent \
- ucomm=$( ps -p $pid -o ucomm= 2> /dev/null ) \
- case "$ucomm" in \
- ssh-agent|ssh|sshd) break ;; \
- esac \
- pid= \
- done \
- [ "$pid" ] || continue \
- fi \
- \
- # Must be owned by the current user unless -a is used \
- # NB: When -a is used, the socket still has to be readable \
- if [ ! "$list_all" ]; then \
- owner=$( stat $stat "$socket" 2> /dev/null ) || \
- continue \
- [ "$owner" = "$current_user" ] || continue \
- fi \
- \
- sockets="$sockets $socket" \
- nsockets=$(( $nsockets + 1 )) \
- export socketpid$nsockets=$pid \
- done \
- sockets="${sockets# }" \
- \
- if [ $nsockets -eq 0 ]; then \
- if [ ! "$noninteractive" ]; then \
- local msg="$ALIASNAME: No agent sockets available" \
- echo "$msg" | ${LOLCAT:-cat} >&2 \
- fi \
- return ${FAILURE:-1} \
- fi \
- if [ $nsockets -eq 1 ]; then \
- cexport SSH_AUTH_SOCK="$sockets" \
- [ "pid" ] && cexport SSH_AGENT_PID=$pid \
- [ "$SSH_AGENT_PID" -a ! "$quiet" ] && # show process \
- [ "$interactive" ] && \
- ps -p "$SSH_AGENT_PID" | ${LOLCAT:-cat} >&2 \
- # dump fingerprints from newly configured agent \
- if ! [ "$quiet" -o "$noninteractive" ]; then \
- [ "$pid" ] && echo " NB: Use " \\\\\
- "\`ssh-agent -k'\'' to kill this agent" \
- if have timeout; then \
- timeout $t ssh-add -l \
- else \
- ssh-add -l \
- fi \
- fi | ${LOLCAT:-cat} >&2 \
- return ${SUCCESS:-0} \
- fi \
- \
- # There is more than one agent available \
- [ "$noninteractive" ] && return ${FAILURE:-1} \
- \
- # \
- # If we do not have dialog(1), just print the possible values \
- # \
- if ! have dialog; then \
- local prefix="%3s" \
- local fmt="$prefix %5s %-20s %s\n" \
- local num=0 choice \
- local identities nloaded \
- \
- sockets=$( command ls -tr $sockets ) # asc order by age \
- printf "$fmt" "" PID USER+NKEYS COMMAND >&2 \
- for socket in $sockets; do \
- num=$(( $num + 1 )) \
- eval pid=\$socketpid$num \
- nkeys=0 \
- identities=$( \
- unset interactive \
- export SSH_AUTH_SOCK="$socket" \
- if have timeout; then \
- timeout $t ssh-add -l \
- else \
- ssh-add -l \
- fi \
- ) && nkeys=$( echo "$identities" | grep -c . ) \
- printf "$fmt" $num: "$pid" \\\\\
- "$( ps -p $pid -o user= )"+"$nkeys" \\\\\
- "$( ps -p $pid -o command= )" | \
- ${LOLCAT:-cat} >&2 \
- done \
- echo >&2 \
- echo -n "Select a number [$num]: " | ${LOLCAT:-cat} >&2 \
- read choice \
- : ${choice:=$num} \
- case "$choice" in \
- ""|*[\!0-9]*) \
- echo "$ALIASNAME: Invalid choice [$choice]" | \
- ${LOLCAT:-cat} >&2 \
- return ${FAILURE:-1} ;; \
- esac \
- if [ $choice -gt $num -o $choice -lt 1 ]; then \
- echo "$ALIASNAME: Choice out of range [$choice]" | \
- ${LOLCAT:-cat} >&2 \
- return ${FAILURE:-1} \
- fi \
- set -- $sockets \
- eval socket=\"\${$choice}\" \
- \
- cexport SSH_AUTH_SOCK="$socket" \
- eval pid=\$socketpid$choice \
- [ "$pid" ] && cexport SSH_AGENT_PID=$pid \
- else \
- local menu_list= \
- \
- sockets=$( command ls -1t $sockets ) # desc order by age \
- have timeout || t= \
- menu_list=$( echo "$sockets" | \
- awk -v t="$t" -v tags="$DIALOG_MENU_TAGS" '\'' \
- { \
- if (++tagn > length(tags)) exit \
- if (\!match($0, /[[:digit:]]+$/)) next \
- pid = ENVIRON["socketpid"tagn] \
- cmd = sprintf("ps -p %u -o user=", pid) \
- cmd | getline user \
- close(cmd) \
- cmd = sprintf("ps -p %u -o command=", pid) \
- cmd | getline command \
- close(cmd) \
- nloaded = 0 \
- cmd = "SSH_AUTH_SOCK=" $0 \
- if (t != "") cmd = cmd " timeout " t \
- cmd = cmd " ssh-add -l" \
- while (cmd | getline identity) { \
- nloaded += identity ~ /^[[:digit:]]/ \
- } \
- close(cmd) \
- printf "'\''\'\''%s\'\''\ \'\''%s\'\''\ \'\''%s\'\'''\''\n", \
- substr(tags, tagn, 1), \
- sprintf("pid %u %s+%u %s", pid, \
- user, nloaded, command), \
- sprintf("%s %s", \
- "SSH_AUTH_SOCK=" $0, \
- "SSH_AGENT_PID=" pid) \
- }'\'' \
- ) \
- \
- local prompt="Pick an ssh-agent to duplicate (user+nkeys):" \
- eval dialog \\\\\
- --clear --title "'\''$ALIASNAME'\''" --item-help \\\\\
- --menu "'\''$prompt'\''" 17 55 9 $menu_list \\\\\
- >&2 2> "$DIALOG_TMPDIR/dialog.menu.$$" \
- local retval=$? \
- \
- # Return if "Cancel" (-1) or ESC (255) \
- [ $retval -eq ${SUCCESS:-0} ] || return $retval \
- \
- local tag="$( dialog_menutag )" \
- cexport $( eval dialog_menutag2help \\\\\
- "'\''$tag'\''" $menu_list ) \
- fi \
- \
- # Attempt to show the running agent \
- [ "$SSH_AGENT_PID" -a ! "$quiet" ] && \
- ps -p "$SSH_AGENT_PID" | ${LOLCAT:-cat} >&2 \
- \
- # Attempt to dump fingerprints from newly configured agent \
- if [ ! "$quiet" ]; then \
- echo "# NB: Use \`$ALIASNAME'\'' to select different agent" \
- echo "# NB: Use \`ssh-agent -k'\'' to kill this agent" \
- if have timeout; then \
- timeout $t ssh-add -l \
- else \
- ssh-add -l \
- fi \
- fi | ${LOLCAT:-cat} >&2 \
- '
- alias sad ssh-agent-dup
- # openkey [-hv]
- #
- # Mounts my F.o thumb
- #
- # NB: Requires eprintf() eval2() have() -- from this file
- # NB: Requires awk(1) df(1) id(1) mount(8) -- from base system
- #
- quietly unalias openkey
- shfunction openkey \
- '__fprintf=$shfunc_fprintf:q' \
- '__eprintf=$shfunc_eprintf:q' \
- '__eval2=$shfunc_eval2:q' \
- '__have=$shfunc_have:q' \
- ' \
- eval "$__fprintf" \
- eval "$__eprintf" \
- eval "$__eval2" \
- eval "$__have" \
- \
- [ "$UNAME_s" = "FreeBSD" ] || \
- { echo "$FUNCNAME: FreeBSD only!" >&2; return 1; } \
- local OPTIND=1 OPTARG flag verbose= sudo= \
- while getopts hv flag; do \
- case "$flag" in \
- v) verbose=1 ;; \
- *) local optfmt="\t%-4s %s\n" \
- eprintf "Usage: $FUNCNAME [-hv]\n" \
- eprintf "OPTIONS:\n" \
- eprintf "$optfmt" "-h" \\\\\
- "Print this text to stderr and return." \
- eprintf "$optfmt" "-v" \\\\\
- "Print verbose debugging information." \
- return ${FAILURE:-1} \
- esac \
- done \
- shift $(( $OPTIND - 1 )) \
- if [ "$( id -u )" != "0" ]; then \
- if have sr; then \
- sudo=sr \
- elif have sudo; then \
- sudo=sudo \
- fi || { \
- eprintf "$FUNCNAME: not enough privileges\n" \
- return ${FAILURE:-1} \
- } \
- fi \
- df -l /mnt | awk '\'' \\
- $NF == "/mnt" { exit found++ } END { exit \!found } \\
- '\'' || ${verbose:+eval2} $sudo mount /mnt || return \
- local nfail=3 \
- while [ $nfail -gt 0 ]; do \
- /mnt/mount.sh -d${verbose:+v} && break \
- nfail=$(( $nfail - 1 )) \
- done \
- [ "$verbose" ] && df -hT /mnt/* | ( awk '\'' \\
- NR == 1 { print > "/dev/stderr"; next } 1 \\
- '\'' | sort -u ) 2>&1 \
- return ${SUCCESS:-0} \
- '
- # closekey [-ehv]
- #
- # Unmounts my F.o thumb
- #
- # NB: Requires eprintf() have() -- from this file
- # NB: Requires awk(1) camcontrol(8) df(1) id(1) umount(8) -- from base system
- #
- quietly unalias closekey
- shfunction closekey \
- '__fprintf=$shfunc_fprintf:q' \
- '__eprintf=$shfunc_eprintf:q' \
- '__have=$shfunc_have:q' \
- ' \
- eval "$__fprintf" \
- eval "$__eprintf" \
- eval "$__have" \
- \
- local OPTIND=1 OPTARG flag eject= verbose= sudo= \
- while getopts ehv flag; do \
- case "$flag" in \
- e) eject=1 ;; \
- v) verbose=1 ;; \
- *) local optfmt="\t%-4s %s\n" \
- eprintf "Usage: $FUNCNAME [-ehv]\n" \
- eprintf "OPTIONS:\n" \
- eprintf "$optfmt" "-e" \\\\\
- "Eject USB media (using "\`"camcontrol eject'\'')." \
- eprintf "$optfmt" "-h" \\\\\
- "Print this text to stderr and return." \
- eprintf "$optfmt" "-v" \\\\\
- "Print verbose debugging information." \
- return ${FAILURE:-1} \
- esac \
- done \
- shift $(( $OPTIND - 1 )) \
- if [ "$( id -u )" != "0" ]; then \
- if have sr; then \
- sudo=sr \
- elif have sudo; then \
- sudo=sudo \
- fi || { \
- eprintf "$FUNCNAME: not enough privileges\n" \
- return ${FAILURE:-1} \
- } \
- fi \
- [ ! -f "/mnt/umount.sh" ] || \
- ${verbose:+eval2} /mnt/umount.sh ${verbose:+-v} || return \
- [ ! "$eject" ] || daN=$( df -l /mnt | awk '\'' \\
- $NF == "/mnt" && match($0, "^/dev/[[:alpha:]]+[[:digit:]]+") { \\
- print substr($0, 6, RLENGTH - 5) \\
- exit found++ \\
- } END { exit ! found } \\
- '\'' ) || daN=$( \
- [ "$sudo" -a "$verbose" ] && \
- echo $sudo camcontrol devlist >&2 \
- $sudo camcontrol devlist | awk '\'' \\
- BEGIN { \\
- camfmt = "^<%s>[[:space:]]+[^(]*" \\
- \\
- disk[nfind = 0] = "da[[:digit:]]+" \\
- find[nfind++] = "USB Flash Disk 1100" \\
- \\
- #disk[nfind] = "device_pattern" \\
- #find[nfind++] = "model_pattern" \\
- } \\
- found = 0 \\
- { \\
- for (n = 0; n < nfind; n++) \\
- { \\
- if (\!match($0, sprintf(camfmt, find[n]))) \\
- continue \\
- devicestr = substr($0, RSTART + RLENGTH + 1) \\
- gsub(/\).*/, "", devicestr) \\
- ndevs = split(devicestr, devices, /,/) \\
- for (d = 1; d <= ndevs; d++) { \\
- if (devices[d] !~ "^" disk[n] "$") \\
- continue \\
- found = 1 \\
- break \\
- } \\
- if (found) break \\
- } \\
- } \\
- found && $0 = devices[d] { print; exit } \\
- END { exit \!found } \\
- '\'' ) || return \
- [ ! -f "/mnt/umount.sh" ] || \
- ${verbose:+eval2} /mnt/umount.sh ${verbose:+-v} || return \
- ! df -l /mnt | \
- awk '\''$NF=="/mnt"{exit found++}END{exit \!found}'\'' || \
- ${verbose:+eval2} $sudo umount /mnt || return \
- [ "$eject" -a "$daN" ] && \
- ${verbose:+eval2} $sudo camcontrol eject "$daN" \
- return ${SUCCESS:-0} \
- '
- # loadkeys [OPTIONS] [key ...]
- #
- # Load my SSH private keys from my F.o thumb. The `key' argument is to the
- # SSH private keyfile's suffix; in example, "sf" for "id_rsa.sf" or "f.o" for
- # "id_rsa.f.o" or "gh" for "id_rsa.gh".
- #
- # For example, to load the Sourceforge.net key, F.o key, and Github key:
- # loadkeys sf f.o gh
- #
- # OPTIONS:
- # -c Close USB media after loading keys.
- # -e Close and eject USB media after loading keys.
- # -h Print this text to stderr and return.
- # -k Kill running ssh-agent(1) and launch new one.
- # -n Start a new ssh-agent, ignoring current one.
- # -t timeout Timeout. Only used if starting ssh-agent(1).
- # -v Print verbose debugging information.
- #
- # NB: Requires closekey() colorize() eprintf() openkey() ssh-agent() quietly()
- # ssh-agent-dup() -- from this file
- # NB: Requires awk(1) kill(1) ps(1) ssh-add(1) -- from base system
- #
- quietly unalias loadkeys
- shfunction ssh_agent ' \
- [ $# -gt 0 ] || set -- -t 1800 \
- eval "$( /usr/bin/ssh-agent -s "$@" 2>&1 | grep -v Agent )" \
- local retval=$? \
- cexport SSH_AUTH_SOCK=$SSH_AUTH_SOCK SSH_AGENT_PID=$SSH_AGENT_PID \
- echo Agent pid $SSH_AGENT_PID >&2 \
- return $retval \
- '
- unalias ssh_agent # [e]shfunction use only
- eshfunction loadkeys \
- '__cexport=$shfunc_cexport:q' \
- '__fprintf=$shfunc_fprintf:q' \
- '__eprintf=$shfunc_eprintf:q' \
- '__have=$shfunc_have:q' \
- '__openkey=$shfunc_openkey:q' \
- '__quietly=$shfunc_quietly:q' \
- '__colorize=$shfunc_colorize:q' \
- '__closekey=$shfunc_closekey:q' \
- '__ssh_agent=$shfunc_ssh_agent:q' \
- '__ssh_agent_dup=$shfunc_ssh_agent_dup:q' \
- ' \
- eval "$__cexport" \
- eval "$__fprintf" \
- eval "$__eprintf" \
- eval "$__have" \
- eval "$__openkey" \
- eval "$__quietly" \
- eval "$__colorize" \
- eval "$__closekey" \
- eval "$__ssh_agent" \
- eval "$__ssh_agent_dup" \
- \
- local OPTIND=1 OPTARG flag \
- local close= eject= kill= new= pid= timeout= verbose= \
- while getopts cehknt:v flag; do \
- case "$flag" in \
- c) close=1 ;; \
- e) close=1 eject=1 ;; \
- k) kill=1 ;; \
- n) new=1 ;; \
- v) verbose=1 ;; \
- t) timeout="$OPTARG" ;; \
- *) local optfmt="\t%-12s %s\n" \
- eprintf "Usage: $FUNCNAME [OPTIONS] [key ...]\n" \
- eprintf "OPTIONS:\n" \
- eprintf "$optfmt" "-c" \\\\\
- "Close USB media after loading keys." \
- eprintf "$optfmt" "-e" \\\\\
- "Close and eject USB media after loading keys." \
- eprintf "$optfmt" "-h" \\\\\
- "Print this text to stderr and return." \
- eprintf "$optfmt" "-k" \\\\\
- "Kill running ssh-agent(1) and launch new one." \
- eprintf "$optfmt" "-n" \\\\\
- "Start a new ssh-agent, ignoring current one." \
- eprintf "$optfmt" "-t timeout" \\\\\
- "Timeout. Only used if starting ssh-agent(1)." \
- eprintf "$optfmt" "-v" \\\\\
- "Print verbose debugging information." \
- return ${FAILURE:-1} \
- esac \
- done \
- shift $(( $OPTIND - 1 )) \
- if [ "$kill" ]; then \
- quietly ssh-agent -k ||: \
- unset SSH_AUTH_SOCK SSH_AGENT_PID \
- echo unsetenv SSH_AUTH_SOCK SSH_AGENT_PID \
- fi \
- if [ "$new" ]; then \
- ssh_agent ${timeout:+-t"$timeout"} || \
- return ${FAILURE:-1} \
- elif quietly kill -0 "$SSH_AGENT_PID"; then \
- : already running \
- elif [ "$SSH_AUTH_SOCK" ] && quietly ssh-add -l; then \
- if have lsof; then \
- cexport SSH_AGENT_PID=$( \
- lsof -t -- "$SSH_AUTH_SOCK" \
- ) \
- else \
- case "$SSH_AUTH_SOCK" in \
- */agent.[0-9]*) \
- pid="${SSH_AUTH_SOCK##*/agent.}" \
- pid="${pid%%[\!0-9]*}" \
- cexport SSH_AGENT_PID=$pid \
- ;; \
- esac \
- fi \
- else \
- if ! ssh_agent_dup -q; then \
- ssh_agent ${timeout:+-t"$timeout"} || \
- return ${FAILURE:-1} \
- fi \
- fi \
- ps -p "$SSH_AGENT_PID" >&2 || return ${FAILURE:-1} \
- local suffix file show= load_required= \
- [ $# -eq 0 ] && load_required=1 \
- for suffix in "$@"; do \
- file="/mnt/keys/id_rsa.$suffix" \
- ssh-add -l | awk -v file="$file" '\'' \\
- gsub(/(^[0-9]+ [[:xdigit:]:]+ | \(.*\).*$)/, "") && \\
- $0 == file { exit found++ } \\
- END { exit \!found } \\
- '\'' && show="$show${show:+|}$suffix" && \
- continue # already loaded \
- load_required=1 \
- break \
- done \
- ssh-add -l | colorize -c 36 \\\\\
- "/mnt/keys/id_rsa\\.($show)([[:space:]]|$)" >&2 \
- [ "$load_required" ] || return ${SUCCESS:-0} \
- openkey ${verbose:+-v} >&2 || return ${FAILURE:-1} \
- [ "$verbose" ] && ssh-add -l >&2 \
- local loaded_new= \
- if [ $# -gt 0 ]; then \
- for suffix in "$@"; do \
- file="/mnt/keys/id_rsa.$suffix" \
- [ -f "$file" ] || continue \
- ssh-add -l | awk -v file="$file" '\'' \\
- gsub(/(^[0-9]+ [[:xdigit:]:]+ | \(.*\).*$)/, \\
- "") && $0 == file { exit found++ } \\
- END { exit \!found } \\
- '\'' && continue \
- ssh-add "$file" >&2 || continue \
- loaded_new=1 \
- show="$show${show:+|}$suffix" \
- done \
- else \
- for file in /mnt/keys/id_rsa.*; do \
- [ -e "$file" ] || continue \
- [ "$file" != "${file%.[Pp][Uu][Bb]}" ] && continue \
- ssh-add -l | awk -v file="$file" '\'' \\
- gsub(/(^[0-9]+ [[:xdigit:]:]+ | \(.*\).*$)/, \\
- "") && $0 == file { exit found++ } \\
- END { exit \!found } \
- '\'' && continue \
- ssh-add "$file" >&2 || continue \
- loaded_new=1 \
- show="$show${show:+|}${file#/mnt/keys/id_rsa.}" \
- done \
- fi \
- [ "$close" ] && closekey ${verbose:+-v} ${eject:+-e} >&2 \
- [ "$loaded_new" ] && ssh-add -l | colorize -c 36 \\\\\
- "/mnt/keys/id_rsa\\.($show)([[:space:]]|$)" >&2 \
- '
- # unloadkeys [OPTIONS] [key ...]
- #
- # Unload my SSH private keys from my F.o thumb. The `key' argument is to the
- # SSH private keyfile's suffix; in example, "sf" for "id_rsa.sf" or "f.o" for
- # "id_rsa.f.o" or "gh" for "id_rsa.gh".
- #
- # For example, to unload the Sourceforge.net key, F.o key, and Github key:
- # unloadkeys sf f.o gh
- #
- # OPTIONS:
- # -a Unload all keys.
- # -c Close USB media after unloading keys.
- # -e Close and eject USB media after unloading keys.
- # -h Print this text to stderr and return.
- # -v Print verbose debugging information.
- #
- # NB: Requires closekey() colorize() eprintf() openkey() quietly()
- # -- from this file
- # NB: Requires awk(1) ps(1) ssh-add(1) -- from base system
- #
- quietly unalias unloadkeys
- shfunction unloadkeys \
- '__fprintf=$shfunc_fprintf:q' \
- '__eprintf=$shfunc_eprintf:q' \
- '__eval2=$shfunc_eval2:q' \
- '__have=$shfunc_have:q' \
- '__openkey=$shfunc_openkey:q' \
- '__quietly=$shfunc_quietly:q' \
- '__colorize=$shfunc_colorize:q' \
- '__closekey=$shfunc_closekey:q' \
- ' \
- eval "$__fprintf" \
- eval "$__eprintf" \
- eval "$__eval2" \
- eval "$__have" \
- eval "$__openkey" \
- eval "$__quietly" \
- eval "$__colorize" \
- eval "$__closekey" \
- \
- local OPTIND=1 OPTARG flag all= close= eject= verbose= \
- while getopts acehv flag; do \
- case "$flag" in \
- a) all=1 ;; \
- c) close=1 ;; \
- e) close=1 eject=1 ;; \
- v) verbose=1 ;; \
- *) local optfmt="\t%-12s %s\n" \
- eprintf "Usage: $FUNCNAME [OPTIONS] [key ...]\n" \
- eprintf "OPTIONS:\n" \
- eprintf "$optfmt" "-a" "Unload all keys." \
- eprintf "$optfmt" "-c" \\\\\
- "Close USB media after loading keys." \
- eprintf "$optfmt" "-e" \\\\\
- "Close and eject USB media after loading keys." \
- eprintf "$optfmt" "-h" \\\\\
- "Print this text to stderr and return." \
- eprintf "$optfmt" "-v" \\\\\
- "Print verbose debugging information." \
- return ${FAILURE:-1} \
- esac \
- done \
- shift $(( $OPTIND - 1 )) \
- local suffix file show= unload_required= \
- if [ "$all" ]; then \
- unload_required=1 \
- shift $# \
- fi \
- for suffix in "$@"; do \
- file="/mnt/keys/id_rsa.$suffix" \
- ssh-add -l | awk -v file="$file" '\'' \\
- gsub(/(^[0-9]+ [[:xdigit:]:]+ | \(.*\).*$)/, "") && \\
- $0 == file { exit found++ } \\
- END { exit \!found } \\
- '\'' || continue # not loaded \
- show="$show${show:+|}$suffix" \
- unload_required=1 \
- break \
- done \
- ssh-add -l | \
- colorize -c 31 "/mnt/keys/id_rsa\\.($show)([[:space:]]|$)" \
- [ "$unload_required" ] || return ${SUCCESS:-0} \
- openkey ${verbose:+-v} || return ${FAILURE:-1} \
- [ "$verbose" ] && ssh-add -l \
- if [ "$all" ]; then \
- ssh-add -D \
- else \
- for suffix in "$@"; do \
- file="/mnt/keys/id_rsa.$suffix" \
- [ -f "$file" ] || continue \
- ssh-add -l | awk -v file="$file" '\'' \\
- gsub(/(^[0-9]+ [[:xdigit:]:]+ | \(.*\).*$)/, \\
- "") && $0 == file { exit found++ } \\
- END { exit \!found } \\
- '\'' || continue \
- ssh-add -d "$file" \
- done \
- fi \
- [ "$close" ] && closekey ${verbose:+-v} ${eject:+-e} \
- [ "$all" ] || ssh-add -l | \
- colorize -c 36 "/mnt/keys/id_rsa\\.($show)([[:space:]]|$)" \
- '
- # dialog_menutag
- #
- # Obtain the menutag chosen by the user from the most recently displayed
- # dialog(1) menu and clean up any temporary files.
- #
- # NB: Requires quietly() -- from this file
- # NB: Requires $DIALOG_TMPDIR -- from this file
- # NB: Requires rm(1) -- from base system
- #
- quietly unalias dialog_menutag
- shfunction dialog_menutag \
- '__quietly=$shfunc_quietly:q' \
- ' \
- eval "$__quietly" \
- \
- local tmpfile="$DIALOG_TMPDIR/dialog.menu.$$" \
- \
- [ -f "$tmpfile" ] || return ${FAILURE:-1} \
- \
- cat "$tmpfile" 2> /dev/null \
- quietly rm -f "$tmpfile" \
- \
- return ${SUCCESS:-0} \
- '
- # dialog_menutag2help $tag_chosen $tag1 $item1 $help1 \
- # $tag2 $item2 $help2
- #
- # To use the `--menu' option of dialog(1) with the `--item-help' option, you
- # must pass an ordered list of tag/item/help triplets on the command-line. When
- # the user selects a menu option the tag for that item is printed to stderr.
- #
- # This function allows you to dereference the tag chosen by the user back into
- # the help associated with said tag (item is discarded/ignored).
- #
- # Pass the tag chosen by the user as the first argument, followed by the
- # ordered list of tag/item/help triplets (HINT: use the same tag/item/help list
- # as was passed to dialog(1) for consistency).
- #
- # If the tag cannot be found, NULL is returned.
- #
- quietly unalias dialog_menutag2help
- shfunction dialog_menutag2help ' \
- local tag="$1" tagn help \
- shift 1 # tag \
- \
- while [ $# -gt 0 ]; do \
- tagn="$1" \
- help="$3" \
- shift 3 # tagn/item/help \
- \
- if [ "$tag" = "$tagn" ]; then \
- echo "$help" \
- return ${SUCCESS:-0} \
- fi \
- done \
- return ${FAILURE:-1} \
- '
- # colorize [-c ANSI] [-e ANSI] pattern
- #
- # Colorize text matching pattern with ANSI sequence (default is `31;1' for red-
- # bold). Non-matching lines are printed as-is.
- #
- # NB: Requires awk(1) -- from base system
- #
- quietly unalias colorize
- shfunction colorize ' \
- local OPTIND=1 OPTARG flag \
- local ti= \
- local te= \
- while getopts c:e: flag; do \
- case "$flag" in \
- c) ti="$OPTARG" ;; \
- e) te="$OPTARG" ;; \
- esac \
- done \
- shift $(( $OPTIND - 1 )) \
- \
- awk -v ti="${ti:-31;1}" -v te="${te:-39;22}" -v pattern="$1" '\'' \
- gsub(pattern, "\033[" ti "m&\033[" te "m")||1 \
- '\'' # END-QUOTE \
- '
- ############################################################ SHELL BEHAVIOR
- #
- # Set prompt style
- #
- set prompt='%{\033[32;1m%}%n@%m %{\033[34;1m%}%c %#%{\033[0m%} '
- #
- # Set the window title for terminals such as "xterm", "rxvt", and others
- #
- if ( $interactive ) then
- settitle $USER@$HOST $HOME
- endif
- #
- # Make new files group-writable by default
- #
- umask 002
- ############################################################ MISCELLANEOUS
- #
- # cvs(1) settings
- #
- setenv CVS_RSH cvs_rsh
- setenv CVSROOT cvs:/repos/projects
- #
- # BitchX(1) settings
- #
- setenv IRCNICK devinteske
- setenv IRCSERVER frenode.net
- setenv IRCNAME dteske
- #
- # jail_build(8) settings
- #
- setenv JAIL_BUILD_DESTDIR /raid1/jails
- #
- # sysrc(8) settings
- #
- setenv SYSRC_VERBOSE 1
- #
- # Override the default password prompt for sudo(8). This helps differentiate
- # the sudo(8) password prompt from others such as su(1), ssh(1), and login(1).
- #
- setenv SUDO_PROMPT '[sudo] Password:'
- #
- # Manual pages with color
- #
- setenv LESS_TERMCAP_mb "${asc}31m" # turn on blinking
- setenv LESS_TERMCAP_md "${asc}31m" # turn on bold (extra bright)
- setenv LESS_TERMCAP_me "${asc}m" # turn off all attributes
- setenv LESS_TERMCAP_so "${asc}1;33;44m" # begin standout mode
- setenv LESS_TERMCAP_se "${asc}m" # exit standout mode
- setenv LESS_TERMCAP_us "${asc}1;34m" # begin underline mode
- setenv LESS_TERMCAP_ue "${asc}m" # exit underline mode
- ############################################################ MAIN
- #
- # Display some wisdom
- # NB: Uncomment to enable
- #
- #?if ( "$interactive" ) then
- #? fortune $KA_FORTUNE
- #?endif
- #
- # Quietly attach to running ssh-agent(1) unless agent already given
- #
- if ( ! $?SSH_AUTH_SOCK ) then
- if ( "$interactive" ) then
- ssh-agent-dup
- else
- ssh-agent-dup -n || :
- endif
- endif
- ################################################################################
- # END
- ################################################################################
Add Comment
Please, Sign In to add comment