2012-02-26 19 views
24

Ben uzak makinede bir komutu çalıştırıyorum:Kumaşın çıkışını baskılamanın kolay yolu?

remote_output = run('mysqldump --no-data --user=username --password={0} database'.format(password)) 

ben çıktısını yakalamak ama hepsi ekrana basılı yok etmek istiyorum. Bunu yapmanın en kolay yolu nedir?

cevap

31

Managing output bölümü tam size göre.

, konsoldan çıktı gizlemek böyle bir şey denemek için: günlüğünden şeyi gizlemek ve kumaş kaçınmak istiyorsanız

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 
17

bu deneyin:

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 örnek sonuçları ise Komut başarısız olduğunda istisnalar atma:

from __future__ import with_statement 
from fabric.api import env,run,hide,settings 

env.host_string = '[email protected]' 
env.key_filename = '/path/to/key.pem' 

def exec_remote_cmd(cmd): 
    with hide('output','running','warnings'), settings(warn_only=True): 
     return run(cmd) 

Bundan sonra, resu komutlarını kontrol edebilirsiniz. Bu örnekte gösterildiği gibi edilmiştir:

 
* 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 
Bu programın konsol çıkış olacaktır

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") 

(kumaştan mesajları orada oturum olduğu dikkate alınmalıdır)

İlgili konular