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

守給你的承諾、 提交于 2019-11-26 23:12:19

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!