In order to collect server information and obtain the results from the fabric:

Asked 2 years ago, Updated 2 years ago, 82 views

How do I get into a remote server with fabric, process it, and bring back the results?

What do you want to do?

1. Log in to a specific directory on the remote server as a specific user to execute a script on the local server.
I don't want to have a script on the remote server.

2. Move the execution result to the local server and erase the file generated to save the execution result.

I'm not really looking for a fabric solution, but I don't think I can do it with fabric.

python fabric

2022-09-30 18:47

2 Answers

If it's in fabric, how about putting the script on the local server and placing it remotely and running it, getting the results from remote to local, and deleting the script that you put in the remote?

Sample put and get fabfile.py
<Dear Quote Site: http://perezvon.hatenablog.com/entry/20091026/1256552181>

 from fabric.api import local, put, get

default_and_get():
local('date>/tmp/local.txt')
put('/tmp/local.txt', '/tmp/remote.txt')
get('/tmp/remote.txt', '/tmp/remote2.txt')

I personally recommend expect or sshpass as it will be done anyway.


2022-09-30 18:47

If you simply want to run a local script remotely, you can use ssh.

 ssh user@host'bash-s'<local_script.sh>results.txt

If you want to give arguments to commands or take over environment variables, you'll need to do something more.

If you use fabric, save the code below to fabfile.py and run it as fab remote:hostname, local script name.

 from fabric.api import run, settings

def remote(host, local_script):
    # read the local script
    with open(local_script, 'rb') asf:
        script=f.read()
    # run it on the remote host
    with settings(host_string=host):
        resp=run(script)
    # save the result into results.txt
    with open('results.txt', 'wb') as f:
        f.write(resp)


2022-09-30 18:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.