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