1

I have a flask app running on my webserver to make calls to different remote servers based on hostname POST'ed from user selection. The webserver flask app makes SSH calls (using paramiko) to user desired hostnames to run a python script.

Currently, I have to manually create a python script for each function I want to call in the backend or else I would run into import errors (since my webserver flask does not have the same python environment as the remote server).

For example, on my backend remote server if I want to call a function from my backend Python module I have to create something like this:

myScript.py:

#!/usr/bin/python3
from myPackage import myModule
myModule.myFunction()

And call it from the webserver flask app like this:

client.exec_command('(cd /myDir; ./myScript.py:)')

My question is, is there a way to stimulate the backend python environment and make calls directly to the python modules, or even some how be able to import the modules from the backend, so I don't have to create a separate script for each function I want to call?

tiger_groove
  • 956
  • 2
  • 17
  • 46
  • 1
    How about retrieving the file with the function you'd like to call directly (at runtime), saving it temporarily and then importing the temporary file regularly? – Jonas Jul 18 '18 at 05:43
  • @Jonas My remote server is installed via python package, I won't be able to just retrieve a single file, because some of the files' function calls are dependent on other modules. Could you give an example? – tiger_groove Jul 18 '18 at 18:06

1 Answers1

1

There are two python interpreter options that may interest you:

  1. If you run it as python -c "print('foobar')" you can pass your script as text and don't create any files on your remotes.
  2. If you run it on remote as python - it will start in an interactive mode and listed your commands from a stdin. I've never done it with paramiko, but as I can see, paramiko supports running commands in the interactive mode.

P.S. Still not sure if I've understood your question correctly, hope this helps.

Fine
  • 2,114
  • 1
  • 12
  • 18