I can run the following two commands for example and get the output locally:
ssh [email protected] ls > testhistory.txt
ssh [email protected] "cat .bash_history" > testhistory.txt
But if I run the following command, the local output is always empty:
ssh [email protected] history > testhistory.txt
If I ssh
to the remote destination and then run the history
command, i get the expected output.
Why does the history
command not output results when run inline with ssh
but the ls
command works normally? What do i need to change to make the history
command output results to local file the way I did the ls
command without having to cat
the .bash_history
file?
history
is an internal command to bash, so you don't want to run the executable history
, you want to run the executable bash
. ls
works because is an executable on its own, usually at /bin/ls
.
Besides that, by default bash disables the history in non-interactive shells, as is the case when you run a remote command.
You can create a shell script in the remote machine to enable it and run history, example:
#!/usr/bin/bash
HISTFILE=~/.bash_history
set -o history
history
Or, if you really want to do all that from the ssh call, you can do:
ssh [email protected] 'echo -e "HISTFILE=~/.bash_history\nset -o history\nhistory" | bash'
Note that it also doesn't take into account the HISTTIMEFORMAT
variable, if you use it in the remote machine, so plan for that.
References: History command inside bash script
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments