Automate ssh connection and execution of program with Python's Paramiko

后端 未结 1 1938
后悔当初
后悔当初 2020-11-28 17:03

I want to automate a specific task using python.
This task includes, among some other things, connecting with ssh to a remote server, and running a specific program (cal

相关标签:
1条回答
  • 2020-11-28 17:15

    There is a library built on top of Paramiko that's perhaps better suited for your needs.

    I am speaking of python fabric (of which I have no association)

    Fabric is a Python (2.5-2.7) library and command-line tool for streamlining the use of SSH for application deployment or systems administration tasks.

    It provides a basic suite of operations for executing local or remote shell commands (normally or via sudo) and uploading/downloading files, as well as auxiliary functionality such as prompting the running user for input, or aborting execution.

    If I have understood your requirement correctly, your code might look something like this.

    from fabric.api import run
    
    @task
    def run_a_out()
        run('echo "some input for a.out" | ./a.out')
    

    And you would execute the remote program with

        fab --hosts=someserver run_a_out
    

    If you wanted to dynamically controll what get's passed into a.out, you can add a parameter to run_a_out() and pass it from the command line.

    In short Fabric provides a higher level API for paramiko with most of it's complexity hidden away.

    0 讨论(0)
提交回复
热议问题