Monday, May 6, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  14] [ 0]  / answers: 1 / hits: 55209  / 2 Years ago, sun, may 1, 2022, 9:37:37

how to run a script automatically in server soon after client system establish ssh connection with the server



for ex:
suppose a user will logon to my computer from another system(connected via lan) using ssh connection. At that time, A script(python or shell) should be run automatically in my system to perform some validation?



How to run the script automatically in server system?


More From » networking

 Answers
3

You can do so by adding the following parameter to your config file (/etc/ssh/sshd_config).




 ForceCommand
Forces the execution of the command specified by ForceCommand, ignoring any command supplied by the client and ~/.ssh/rc if present. The command is invoked by using the user's login shell
with the -c option. This applies to shell, command, or subsystem execution. It is most useful inside a Match block. The command originally supplied by the client is available in the
SSH_ORIGINAL_COMMAND environment variable. Specifying a command of “internal-sftp” will force the use of an in-process sftp server that requires no support files when used with
ChrootDirectory.



An other option is to use the .ssh/rc files on a per user basis.



To use the ForceCommand method you just add ForceCommand /usr/bin/ownscript at the bottom of the file /etc/ssh/sshd_config (on the server).



The script looks like this:



#!/bin/bash
#Script file for ssh
#
#put your commands here
echo "test" > /tmp/test.txt
#
#exit by calling a shell to open for the ssh session
/bin/bash


Don't forget to chmod the script sudo chmod +x /usr/bin/ownscript


[#27754] Tuesday, May 3, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rmodmi

Total Points: 390
Total Questions: 122
Total Answers: 111

Location: Venezuela
Member since Mon, Oct 18, 2021
3 Years ago
;