How to execute a process remotely using python

后端 未结 3 1206
清歌不尽
清歌不尽 2020-11-30 02:31

I want to connect to and execute a process on a remote server using Python. I want to be able to get the return code and stderr (if any) of the process. Has anyone ever do

3条回答
  •  半阙折子戏
    2020-11-30 02:57

    Use the ssh module called paramiko which was created for this purpose instead of using subprocess. Here's an example below:

    from paramiko import SSHClient
    client = SSHClient()
    client.load_system_host_keys()
    client.connect("hostname", username="user")
    stdin, stdout, stderr = client.exec_command('program')
    print "stderr: ", stderr.readlines()
    print "pwd: ", stdout.readlines()
    

    UPDATE: The example used to use the ssh module, but that is now deprecated and paramiko is the up-to-date module that provides ssh functionality in python.

提交回复
热议问题