I am running a command on the remote machine:
remote_output = run('mysqldump --no-data --user=username --password={0} database'.format(password))
I would like to capture the output, but not have it all printed to the screen. What's the easiest way to do this?
It sounds like Managing output section is what you're looking for.
To hide the output from the console, try something like this:
from __future__ import with_statement from fabric.api import hide, run, get with hide('output'): run('mysqldump --no-data test | tee test.create_table') get('~/test.create_table', '~/test.create_table')
Belows is the sample results:
No hosts found. Please specify (single) host string for connection: 192.168.6.142 [192.168.6.142] run: mysqldump --no-data test | tee test.create_table [192.168.6.142] download: /home/quanta/test.create_table <- /home/quanta/test.create_table
Try this if you want to hide everything from log and avoid fabric throwing exceptions when command fails:
from __future__ import with_statement from fabric.api import env,run,hide,settings env.host_string = 'username@servernameorip' env.key_filename = '/path/to/key.pem' def exec_remote_cmd(cmd): with hide('output','running','warnings'), settings(warn_only=True): return run(cmd)
After that, you can check commands result as shown in this example:
cmd_list = ['ls', 'lss'] for cmd in cmd_list: result = exec_remote_cmd(cmd) if result.succeeded: sys.stdout.write('\n* Command succeeded: '+cmd+'\n') sys.stdout.write(result+"\n") else: sys.stdout.write('\n* Command failed: '+cmd+'\n') sys.stdout.write(result+"\n")
This will be the console output of the program (observe that there aren't log messages from fabric):
* Command succeeded: ls Desktop espaiorgcats.sql Pictures Public Videos Documents examples.desktop projectes scripts Downloads Music prueba Templates * Command failed: lss /bin/bash: lss: command not found
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With