Search code examples
python-3.xcronparamikossh-keys

Paramiko, private keys and cron


I am having some issues while trying to setup a paramiko scp using private public keys.

The problem is not so much related to the paramiko itself, I think, but the fact this is launched in a Cron (user cron (crontab -e)).

So the script works from the normal terminal, but it does not from the cron. I tried to specify the exact location of the private key (key_filename="/home/myuser/.ssh/id_rsa") when calling the method connect. It returns back the following error: "Not a valid RSA private key file".

On the other hand, in the cron, I tried to declare the terminal to use:

SHELL=/bin/bash
PATH=... (all the typical values)
HOME=/home/myuser

Also tried to source the $HOME/profile prior to launching the task.

It keeps failing.

Either making the cron execution environment to have all the variables as a normal bash or being able to properly specify to paramiko the location of the private key would make it, but all the things I tried did not work.

I also tried: Paramiko can not access private key But it did not work.

And this problem Paramiko: "not a valid RSA private key file" is not applicable, because the script works when launched from a normal terminal with that user. It fails with the cron.

Any clue?

Python3 Paramiko 2.6.0 Ubuntu 20.4.2 LTS


Solution

  • Thanks to Martin Prikryl, after activating the paramiko logging, which reported an error about something not implemented I ended here:

    Getting Oops, unhandled type 3 ('unimplemented') while connecting SFTP with Paramiko

    By applying the private key as per that post (and making a conversion as per this other post: Paramiko: "not a valid RSA private key file" Note I am using the Paramiko 2.6.0)...

    Now it works!!

    Thanks, Martin!