Skip to content

Instantly share code, notes, and snippets.

@snt
Created August 24, 2013 07:32
Show Gist options
  • Save snt/6326669 to your computer and use it in GitHub Desktop.
Save snt/6326669 to your computer and use it in GitHub Desktop.
Record a ssh log in Cygwin. Cygwin is slow on creating new process with `$( ... )`, so `tee` and add `$(date)` on each line would be extremely slow. run `sshlog` instead of `ssh`
function sshlog () {
if [ "x$1" = "x" ]; then
echo "simple logging wrapper for ssh."
echo "use as usual ssh"
ssh
else
TARGET=$1
mkdir -p ~/ssh-log
LOGFILE=~/ssh-log/$TARGET.$(date +%Y-%m-%d_%H.%M.%S).log
ssh $@ | python $HOME/timestamp-logger.py $LOGFILE
echo
echo "written log: $LOGFILE"
echo
fi
}
import sys, datetime
logfile = open(sys.argv[1], "w")
stdin = sys.stdin
print "writing log:", sys.argv[1]
while 1:
try:
c = stdin.read(1)
if not c:
break
sys.stdout.write(c)
sys.stdout.flush()
logfile.write(c)
if c == "\n":
logfile.flush()
logfile.write(datetime.datetime.now().strftime("[%a %b %d %H:%M:%S.%f]"))
except EOFError:
break
logfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment