Last active
December 14, 2015 01:52
-
-
Save flatcap/3c42326abeb1197ee714 to your computer and use it in GitHub Desktop.
automatic ssh logger - http://www.commandlinefu.com/commands/view/14883
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A wrapper around ssh to automatically provide logging and session handling. | |
# This function runs ssh, which runs screen, which runs script. | |
# Requirements: | |
# * Log sessions on a remote server | |
# * Transparent - nothing extra to type | |
# * No installation - nothing to copy to the server beforehand | |
# Features: | |
# * Function wrapper delegating to ssh | |
# so nothing to remember | |
# uses .ssh/config as expected | |
# passes your command line option to ssh | |
# * Self-contained: no scripts to install on the server | |
# * Uses screen(1), so is: | |
# detachable | |
# re-attachable | |
# shareable | |
# * Records session using script(1) | |
# * Configurable log file location, which may contain variables or whitespace | |
# L="$HOME" # local variable | |
# L="\$HOME" # server variable | |
# L="some space" | |
# Limitations: | |
# * Log dir/file may not contain '~' (which would require eval on the server) | |
function ssh() | |
{ | |
# Date format: YYYY-MM-DD_HH:MM | |
local DATE="$(date +%F_%H:%M)" | |
# Directory for script logs | |
# Quoted with '' to prevent shell expansion on local host | |
local LOG_DIR='$HOME/logs' | |
# Log files are named by date and local user | |
local LOG_FILE="${LOG_DIR}/${DATE}-${USER}" | |
# Optionally, save script's timing information | |
local TIMING="--timing=\"${LOG_FILE}-timing\"" | |
# add some commands to the ssh command line | |
# Create the log directory | |
# Start script within a screen session | |
/usr/bin/ssh -t "$@" "mkdir --parents \"$LOG_DIR\"; screen -xRR -S $USER script --flush ${TIMING} \"$LOG_FILE\"" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment