How can I automatically run a command on the local terminal after exiting a ssh connection? Is there any hook or event that could be handled for this?
Asked
Active
Viewed 3,692 times
10
Gilles 'SO- stop being evil'
- 829,060
B Faley
- 4,343
- 11
- 39
- 48
1 Answers
13
Leverage an alias or better a function.
For example:
ssh () { command ssh "$@"; echo foobar; }
Now, you can run:
ssh mysite
after you exit from the ssh session, echo foobar will be run.
Change echo foobar with the actual command you need to run, and of course you can tack multiple commands if you want.
To make the function definition permanent, put it in your ~/.bashrc.
Also note that, it might not always be desired to have the function named as ssh when you want to explicitly use the external ssh. In that case, you can use any one of the following to skip the ssh function to get external ssh binary:
command ssh mysite
or rename the function to something else e.g. sshfunc:
sshfunc () { ssh "$@"; echo foobar; }
heemayl
- 56,300
ssh user@host 'cd /somewhere/ && tar cf - | gzip -c -' > local_backup_of_somewhere.tgz: this will be wrong as it will contain "foobar" at the end...) – Olivier Dulac Oct 10 '16 at 09:46