击:如何干净地登录SSH / bash的输出处理的行? [英] Bash: how to cleanly log processed lines of ssh/ bash output?

查看:281
本文介绍了击:如何干净地登录SSH / bash的输出处理的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了发球和grep一个linux bash脚本到的日志和时间戳的我以我的各种SSH会话的操作。它的工作原理,但记录的行有时一起混合,充满了控制字符。我怎样才能正确地逃脱控制和其他字符在原来的会议不可见,并分别登录各行?

I wrote a linux bash script with tee and grep to log and timestamp the actions I take in my various ssh sessions. It works, but the logged lines are mixed together sometimes and are full of control characters. How can I properly escape control and other characters not visible in the original sessions and log each line separately?

我学习bash和linux的接口,因此任何其他建议,以改善该脚本会非常欢迎!

I am learning bash and the linux interface, so any other suggestions to improve the script would be extremely welcome!

下面是我的脚本(用作SSH命令的封装):

Here is my script (used as a wrapper for the ssh command):

#! /bin/bash
logfile=~/logs/ssh.log
desc="sshlog ${@}"
tab="\t"
format_line() {
    while IFS= read -r line; do
        echo -e "$(date +"%Y-%m-%d %H:%M:%S %z")${tab}${desc}${tab}${line}"
    done
}
echo "[START]" | format_line >> ${logfile}
# grep is used to filter out command line output while keeping commands
ssh "$@" | tee >(grep -e '\@.*\:.*\$' --color=never --line-buffered | format_line >> ${logfile})
echo "[END]" | format_line >> ${logfile}

这是在日志文件中jarbled输出的截图:

And here is a screenshot of the jarbled output in the log file:

该解决方案的说明:蒂亚戈的回答照顾非打印字符的非常好。不幸的是,我只是意识到,混杂是由退格造成的,使用命令完成的上下键。即,字符被输送到只要它们出现grep和未行由行。我将不得不问这个在<一个href=\"http://stackoverflow.com/questions/24462930/bash-duplicate-input-output-from-interactive-scripts-only-in-complete-lines-af\">another问题。

A note on the solution: Tiago's answer took care of the nonprinting characters very well. Unfortunately, I just realized that the jumbling is being caused by backspaces and using the up and down keys for command completion. That is, the characters are being piped to grep as soon as they appear, and not line-by-line. I will have to ask about this in another question.

更新: 想出一个办法来(几乎总是)处理了/下完成,退格完成,并控制字符。

Update: I figured out a way to (almost always) handle up/down completion, backspace completion, and control characters.

推荐答案

您可以删除这些字符:

perl的-lpe的/ [^ [:打印:]] // G'

不过滤:

perl -e 'for($i=0; $i<=255; $i++){print chr($i);}' | cat -A
^@^A^B^C^D^E^F^G^H^I$
^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\^]^^^_ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~^?M-^@M-^AM-^BM-^CM-^DM-^EM-^FM-^GM-^HM-^IM-^JM-^KM-^LM-^MM-^NM-^OM-^PM-^QM-^RM-^SM-^TM-^UM-^VM-^WM-^XM-^YM-^ZM-^[M-^\M-^]M-^^M-^_M- M-!M-"M-#M-$M-%M-&M-'M-(M-)M-*M-+M-,M--M-.M-/M-0M-1M-2M-3M-4M-5M-6M-7M-8M-9M-:M-;M-<M-=M->M-?M-@M-AM-BM-CM-DM-EM-FM-GM-HM-IM-JM-KM-LM-MM-NM-OM-PM-QM-RM-SM-TM-UM-VM-WM-XM-YM-ZM-[M-\M-]M-^M-_M-`M-aM-bM-cM-dM-eM-fM-gM-hM-iM-jM-kM-lM-mM-nM-oM-pM-qM-rM-sM-tM-uM-vM-wM-xM-yM-zM-{M-|M-}M-~M-^?

过滤:

perl -e 'for($i=0; $i<=255; $i++){print chr($i);}' | perl -lpe 's/[^[:print:]]//g'  | cat -A
$
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~$

说明:


  1. 我打印整个ASCII表:

  1. I am printing the whole ASCII table with:

的perl -e'为($ i = 0; $ I&LT; = 255; $ I ++){打印CHR($ I);}

我确定非打印字符有:

猫-A

我过滤非打印字符有:

perl的-lpe的/ [^ [:打印:]] // G'

编辑:在我看来,你需要删除ANSI字符颜色:

It seems to me that you need to remove ANSI color chars:

例如:

 perl -MTerm::ANSIColor -e 'print colored("yellow on_magenta","yellow on_magenta"),"\n"'| sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | perl -lpe 's/[^[:print:]]//g'

适应您的code:

Adapting to your code:

format_line() {
    while IFS= read -r line; do
        line=$(sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" <<< "$line")
        line=$(perl -lpe 's/[^[:print:]]//g' <<< "$line")
        echo -e "$(date +"%Y-%m-%d %H:%M:%S %z")${tab}${desc}${tab}${line}"
    done
}

我也编辑你的的grep 命令:

ssh "$@" | tee >(grep -Po '(?<=\$).*' --color=never --line-buffered | format_line >> ${logfile})

下面我的测试输出:

Below the output of my test:

2014-06-26 10:11:10 +0100   sshlog tiago@localhost  [START]
2014-06-26 10:11:15 +0100   sshlog tiago@localhost  whoami
2014-06-26 10:11:16 +0100   sshlog tiago@localhost  exit
2014-06-26 10:11:16 +0100   sshlog tiago@localhost  [END]

这篇关于击:如何干净地登录SSH / bash的输出处理的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆