1

I have a use-case, where I need to run Linux commands on a remote machine. Currently, I am using Fabric to achieve the functionality. However, I was wondering if it would be better to use Shell script rather than Python.

My use case is to set up some services, set up dbs in MySQL, and additionally create some scripts which would then be executed on the remote machine. For now, I have about 50-60 lines of commands embedded in Fabric calls.
Everything has to be executed on the remote machine, and for the same, I have created a connection to the machine using Fabric, and I run commands with a run/sudo function. For all the different lines of commands, I am using a separate run method.

If I have to use a shell script, I would further have two ways:

  1. Use a Fabric call to run the script on the remote machine.
  2. Make the script ssh into the other machine, and run it there.

What would be the best Pythonic way to achieve the functionality?

Milan
  • 1,743
  • 2
  • 13
  • 36
Harman
  • 751
  • 1
  • 9
  • 31
  • 2
    Your question is too broad to be answered in definitive manner. There are lots of things to consider, both having its pros and cons. For example, what's the complexity of algorithm you'll be trying to implement? Does it require any / simple / complex remote-to-local communication / sync? How often do you invoke it, i.e. would ssh connection initiation be too much time and CPU power to waste, or you can use SSH master connection, or similar functionality provided by Fabric? Last, but not least, the maintainability - what would be easier to support/debug - raw shell script or python?.. – GreyCat Apr 17 '15 at 01:25
  • @GreyCat Thanks for the response. I have edited my question, to add some additional info that would be required to answer the question. However, I couldn't understand your reference to ssh communication by "_simple / complex remote-to-local communication / sync_". Please let me know what other info should I add in order to make the question more informative. – Harman Apr 17 '15 at 08:53
  • "Simple / complex communication" was basically one of the main points ;) If your process has lots of communication between local and remote machine, i.e. you launch some remote command, get output, and then decide to do next remote command by running some local algorithm, that's where Fabric would shine. On the other hand, if you just run commands continuously without any logic, this is obviously easy to wrap in all in one remote shell script. Anyway, that's still a long way for a good specification for a definitive answer... – GreyCat Apr 17 '15 at 16:16
  • @GreyCat Thanks for trying to help. For now, I think I'm going to continue with Fabric. – Harman Apr 22 '15 at 05:43

1 Answers1

0

Have you considered using paramiko ?

Here's a simple example from Brandon Rhodes 'Foundations of Python Network Programming':

import paramiko

class AllowAnythingPolicy(paramiko.MissingHostKeyPolicy):
        def missing_host_key(self, client, hostname, key):
                return
client = paramiko.SSHClient()
client.set_missing_host_key_policy(AllowAnythingPolicy())
client.connect('127.0.0.1', username='username', password='python')

for command in 'echo "Hello, world!"', 'uname -a', 'uptime':
        stdin, stdout, stderr = client.exec_command(command)
        stdin.close()
        print repr(stdout.read())
        stdout.close()
        stderr.close()

client.close()

Cheers, Arthur

Arthur M
  • 447
  • 5
  • 21
  • 1
    Thanks for the response, but my very question is whether to use a Python call or Linux script. – Harman Apr 17 '15 at 09:30
  • Ah OK! I guess both have advantages and disadvantages. I found there were some good points made here: [Shell vs Python](http://stackoverflow.com/questions/796319/strengths-of-shell-scripting-compared-to-python) . My view on it is that since you are setting up DBs you might as well make use of python libraries; you could also include more robust error handling. – Arthur M Apr 17 '15 at 09:45
  • Thanks for the link mate ! I'm continuing with Python, for now. – Harman Apr 22 '15 at 05:45