There is an elegant way to solve this problem - already built into ssh:
1.) Generate a public/private key pair for ssh authentication on your client machine:
Code:
$ ssh-keygen -t rsa
If you want, you can provide an empty password for the key, in that case any user with access to the key file can start the init script.
2.) Transfer the public key (*.pub) to the server and attach it to the authorized_keys file of the user "test"
Code:
$ cat insert_name_of_key_here.pub>> ~/.ssh/authorized_keys
3.) Make testing script on the server. Make it executable and put it in a place where you will find it again. I used the following to test:
Code:
#!/bin/bash
echo -n "Enter text: "
read inp
echo "Text entered: $inp"
echo $inp >/tmp/testdatei
4.) Edit the authorized_keys file on the server. Its last line will look something like this:
Code:
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA0OdXOI7Do22URpJXEYiRgV0Vd5/NXvyziwbuaOpGX4Ww2Knheci
jwcjeiwjojicojwjecjiojweiojcijwjecjijweioioxkGIUGIUGIUjJHJPPOOoopoiasaljceiwoehchwohchowlUK4a
guiNMp01KvQxPrrjw== user@client_machine
Add the following code on the same line in front of ssh-rsa:
Code:
command="/path/to/script"
Done.
Now you can use the private key file you generated in step 1 to login to your server:
Code:
$ ssh -i private_key_file user@server
Whenever you connect using this private key file, the script will be interactively executed on the server machine. When the script terminates, the ssh session will end.
I read this in some tutorial but I cannot find the link now. This should also be explained in some ssh or sshd man-page.
Lotharster