Advertisement
Guest User

Untitled

a guest
Jun 27th, 2014
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.91 KB | None | 0 0
  1. #!/bin/bash
  2. # A wrapper for the ssh command that produces a timestamped log of all ssh commands
  3. # logfile=~/logs/ssh/ssh-`date +%Y-%m-%d-%H%M%S%z`.log
  4. logfile=~/logs/ssh.log
  5. # identifier for the session
  6. description="sshlog-${$} ${@}"
  7. tab="\t"
  8. format_line() {
  9.     while IFS= read -r line; do
  10.         # Filter control and nonprinting characters out
  11.         line=$(sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" <<< "$line")
  12.         line=$(perl -lpe 's/[^[:print:]]//g' <<< "$line")
  13.         # add current timestamp
  14.         echo -e "$(date +"%Y-%m-%d %H:%M:%S %z")${tab}${description}${tab}${line}"
  15.     done
  16. }
  17. echo "Logging: ${description}"
  18. echo "[START]" | format_line >> ${logfile}
  19. # grep is used to filter out command line output while keeping commands
  20. ssh "$@" | tee >(grep -Eo '[\w\[]+.*?[:\[].*?[\$#].*?$' --color=never --line-buffered | format_line >> ${logfile})
  21. echo "[END]" | format_line >> ${logfile}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement