Forums

Use paramiko to run shell commands

I'm creating a local python program which needs to run commands on PA (specifically, run nikola static site build commands). I'm trying to figure out if I can use paramiko (or something else) to get ssh access to execute commands as if I'm running a bash console. Everything I see is about connecting to my database or doing file transfer. Can you point me to relevant guidance or examples. Thanks, --Don

Probably the best library to use for that is Fabric, which is maintained by the same person as Paramiko, and offers a higher-level interface. It's normal usage is via a kind of file full of steps to run on a remote server called a fabfile, but you can also use it to directly embed code that logs on to remote machines via SSH and runs commands on them using its API. The documentation is really good, too.

Great! THANKS!

No problem :-)

I've been working with Fabric and it looks great. That said, it may not solve my 'real' problem. I'm trying to create a static website using Nikola. 'Users' would essentially access only static files (that works nicely). The content developers need access to 'the equivalent of' the backend to upload files, run bash commands, etc. My solution to allowing that for my editors was to create a small app they would run locally on their own machine. The app would internally open an ssh connection and run the necessary commands for them.

This brings up the problem of how they (the app) does ssh login. I've created a keyfile for my use, but I'd rather they have their own keyfile (and thus login) than give them a copy of my keyfile (if that is even possible).
From the docs, this doesn't seem possible.

Am I missing something in how to do this? Is it necessary to give them my keyfile (or embed in the app)? Would that even work?

I'm not sure how to proceed. Thx, --Don

Do you just need to trigger the build of the static content? We use nikola on our help pages and publish it using bare repo and post-receive hook. See https://github.com/pythonanywhere/help_pages/blob/master/README.md how we do it.

Interesting idea, but I'm not sure if it will work or not. I need to learn a bit about using git in that way (we do maintain all else in git). We have a number of shortcodes we have developed to deal with things like creating complex page structures, a large number of images/galleries, controlling access, ... Presumably, if I created some sort of command sequence in git that was picked up by additions within Nikola and there were no errors or responses that needed to be dealt with before deploy, it might work, but that sounds awfully complicated to get around the lack of appropriate access to PA.

so it is possible to create multiple keys and add all of those to the authorized keys list.

so probably just do that? however, note that with that key, they can ssh into your PythonAnywhere account, and have access to all files etc there.