Created
August 12, 2014 12:47
-
-
Save jacobsalmela/17a8ff053cba7f1d3cd3 to your computer and use it in GitHub Desktop.
Real-time SSH history monitor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#----------AUTHOR------------ | |
# Jacob Salmela | |
# 15 November 2013 | |
# https://github.com/jakesalmela/ | |
#----------RESOURCES--------- | |
# http://askubuntu.com/questions/80371/bash-history-handling-with-multiple-terminals | |
# http://www.csgnetwork.com/epochtime.html | |
#---------DESCRIPTION-------- | |
# This script can be run on a local machine. | |
# It allows you to view the commands that are entered on the remote machine. | |
# It also saves the output as a log file | |
# | |
# In order for this to work properly, the remote host needs to have some settings in ~/.bash_profile | |
# | |
# export PROMPT_COMMAND='history -a' | |
# | |
# I also like to have it timestamped: | |
# | |
# HISTTIMEFORMAT="%Y-%m-%d %T " | |
# | |
# If the above was added, the output on the local machine will show the timestamp as seconds since the epoch. | |
# If reviewing the logs later, you will probably want to convert it | |
# If you run the history command, it will show the date properly formatted, but viewing the history file directly shows time since the epoch | |
# | |
# For best results, use SSH keys to avoid having to enter a password | |
#-----------USAGE------------ | |
# To run: | |
# | |
# ./real-time-history-monitor-over-ssh.sh <remoteuser> <remotehost> | |
#----------VARIABLES--------- | |
# Remote user for connecting via SSH | |
remoteUser=$1 | |
# Remote host--IP or hostname | |
remoteHost=$2 | |
# Change these according to your environment: | |
# Log file location | |
logLoc="/Users/Shared/" | |
#----------FUNCTIONS--------- | |
################################## | |
function monitorCmdsOnRemoteHost() | |
{ | |
# Connect to the remote host over SSH and tail the .bash_history file continuously. | |
# On the local machine, view the output and save it to a file at the same time (via tee) | |
echo "Connecting as $remoteUser to $remoteHost, tailing the history file, and saving the output locally..." | |
ssh $remoteUser@$remoteHost 'tail -f ~/.bash_history' | tee $logLoc/hisshtory.log | |
echo "Connection closed. Log file saved to: $logLoc" | |
} | |
#---------------------------------# | |
#-------------SCRIPT--------------# | |
#---------------------------------# | |
monitorCmdsOnRemoteHost |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment