如何记录通过ssh发送的非交互式bash命令 [英] How to log non-interactive bash command sent through ssh
本文介绍了如何记录通过ssh发送的非交互式bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在通过ssh发送命令:
I'm sending a command through ssh:
ssh server.org 'bash -s' << EOF
ls -al
whoami
uptime
EOF
如何将其登录到系统(远程服务器)中?我想将这些命令记录在某个文件(.bash_history或/tmp/log)中.
How to log it in the system (remote server)? I'd like to log those commands in some file (.bash_history or /tmp/log).
我尝试将以下行添加到sshd_config:
I've tried to add the line below to sshd_config:
ForceCommand if [[ -z $SSH_ORIGINAL_COMMAND ]]; then bash; else echo "$SSH_ORIGINAL_COMMAND" >> .bash_history; bash -c "$SSH_ORIGINAL_COMMAND"; fi
但是它仅记录"bash -s".
But it logs "bash -s" only.
我将不胜感激.
推荐答案
-
如果可以使用给定的命令,我们可以添加必要的附加内容以在开始和结束时启用和记录命令历史记录,例如e.g.
If it suffices to work with the given command, we can put the necessary additions to enable and log command history at the beginning and end, e. g.
ssh server.org bash <<EOF set -o history ls -al whoami uptime history|sed 's/ *[0-9]* *//' >>~/.bash_history EOF
-
或者我们可以将它们放入非常长的
ForceCommand
行中:… if [[ "$SSH_ORIGINAL_COMMAND" == bash* ]]; then echo "set -o history"; cat; echo "history|sed 's/ *[0-9]* *//' >>~/.bash_history"; else cat; fi | bash -c "$SSH_ORIGINAL_COMMAND"; fi
这篇关于如何记录通过ssh发送的非交互式bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文