Last active
February 3, 2025 04:19
-
-
Save adamency/09764f8a94bf5039987f401fd4e897a8 to your computer and use it in GitHub Desktop.
Display History of Remote Machine through SSH
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
temp_hist_file="/tmp/remote-host.bash_history" | |
ssh <user>@<remote-host> -i <private_key_for_remote-host> "cat ~/.bash_history" > "$temp_hist_file" | |
(history -cr "$temp_hist_file" ; history) | |
#rm "$temp_hist_file" | |
# Original source: https://unix.stackexchange.com/a/316120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Made for bash. Easy to adapt for other shells.
The main point of this gist is too show the correct solution because no clear solution for this problem is easily found online.
Pitfalls:
ssh user@host "history"
ashistory
is a bash built-in and not a binary in the path. Built-ins are only loaded during interactive sessions.ssh user@host "bash -ic 'history'"
cause issues because it cannot find a TTYex. error:
ssh -t user@host
is not guaranteed to work for all ssh connections (issues can arise when local and remote machines don't use the same OS, e.g. Windows -> Linux.HISTFILE=$temp_hist_file ; history
does not work since the env var is only used at shell startup timehistory -cr "$temp_hist_file" ; history
without parentheses will affect the real bash history of the current local user, which is unwanted.