F
F
Fenix9572016-09-21 02:17:40
linux
Fenix957, 2016-09-21 02:17:40

Execute command after ssh session ends?

The situation is like this. A person connects via ssh, performs the actions that he needs and disconnects from the server. How to automatically execute some command after turning it off?
There is an option when connecting to do the same when disconnecting
---------------
monitoring user connections
Settings in the /etc/ssh/sshrc or ~/.ssh/rc files allow you to perform
some actions when user registration.
Any shell commands can be used here.
For example, let's send an email notification to the administrator that a user has logged into the system via SSH:
# vi /etc/ssh/sshrc
echo $(date) $SSH_CONNECTION $USER $SSH_TTY | mail -s "ssh login" [email protected]
Backup example
Generating a key pair (private and public):
$ sudo ssh-keygen -t rsa -C 'remote backup'
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
/home/user/.ssh/id_rsa_backup
Add the public key to the list of authorized keys on the remote
system:
$ ssh remotehost "umask 077; cat > . ssh/authorized_keys" < .ssh/id_rsa_backup.pub
Then edit the authorized_keys (the '-t' switch should be used when
running programs that require a pseudo-terminal to run):
$ ssh -t remotehost vi .ssh/authorized_keys
from="192.168.0.*,212.34.XX.YY",command="cd /work; tar cvf - ./* | bzip2 -9",
no-pty,no-agent-forwarding,no-X11-forwarding ,no-port-forwarding ssh-rsa AAAA[...]
And start the backup procedure:
$ ssh -i .ssh/id_rsa_backup remotehost > ~/backup/work-`date +%d%m%Y`.tar. bz2 2>/dev/null
The /work directory on the remotehost server will be saved to
~/backup/work-11052008.tar.bz2.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-09-21
@Fenix957

googled for you.
unix.stackexchange.com/questions/136548/force-comm...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question