This is an example of how to execute a remote command using Dancer, Net::SSH2 and fork. It is NOT secure at all, it is just to have an idea and to get some lines of code to implement a function to execute a remote command.
How to send the public key to the remote servers?
To allow the remote server accept the connection, it is required this server knows and authorize the key. This is done by send the public key to the remote host execute this command:
cat ~/.ssh/id_rsa.pub | ssh $user@$server 'cat >> ~/.ssh/authorized_keys'Or this one:
ssh-copy-id -i ~/.ssh/id_rsa.pub $user@$server
There are some comments about this code:
- The password variable may be optional.
- Due to access restriction to the private key I copy it to a directory where the application can read it. Make sure it is only accessible from the dancer application.
- May be good idea to have a function to parse the command and make sure it does not have malicious commands such as 'rm'.
- I tried this code with threads but there are modules not thread-safe so I recommend to use fork instead.