2012-11-15 20 views
5

समाप्त करने से पहले सो मैं एक दूरस्थ मशीन में SSH करने अजगर के paramiko मॉड्यूल का उपयोग कर रहा है और राल/फ़ाइलों की एक बहुत (14K फ़ाइलें और 60 + डेटा की gigs से अधिक) के साथ एक फ़ोल्डर ज़िप करने के लिए कारण बनता है। परिणामस्वरूप ज़िप लगभग 10 गीगा है। अब मैं बिना किसी समस्या के मशीन से सीधे ज़िप/टैर को कमांड चला सकता हूं। हालांकि, जब मैं एसएसएच क्लाइंट के exec_command के माध्यम से एक ही कमांड चलाने की कोशिश करता हूं, तो यह थोड़ा सा चलता है, लेकिन आखिरकार रिमोट मशीन पर ज़िप प्रक्रिया सो जाती है। और recv_exit_status बस अनिश्चित काल तक लटका है। यहां मैं जिस कोड का उपयोग कर रहा हूं वह है:चल आदेश exec_command प्रक्रिया

stdin, stdout, stderr = ssh.exec_command('cd myDirectory; tar -zcvf output.tgz *') 
status = stdout.channel.recv_exit_status() 

मैंने ज़िप का उपयोग करने का भी प्रयास किया।

stdin, stdout, stderr = ssh.exec_command('cd myDirectory; find -name "*.gz" | zip output.zip [email protected]') 
status = stdout.channel.recv_exit_status() 

दोनों मामलों में, यदि मैं सीधे रिमोट मशीन से कमांड चलाता हूं, तो यह ज़िप/टैरिंग को समाप्त करता है। परिणाम फ़ाइल 9 गीगा की तरह है। लेकिन जब मैं इसे paramiko से आज़माता हूं, तो यह शुरू होता है, आधा रास्ता (6ish गीग) से अधिक चला जाता है और फिर प्रक्रिया सो जाती है!

मैं शीर्ष का उपयोग कर दूरस्थ मशीन पर प्रक्रियाओं पर नजर रखी है, और ज़िप/राल प्रदर्शन शुरू हो जाएगा, लेकिन यह अंततः समाप्त करने से पहले सोने के लिए जाना जाएगा। और अजगर लिपि अनिश्चित काल तक लटका होगा।

कोई विचार यह क्यों हो रहा है?

+0

कृपया मेरी अद्यतन उत्तर देखें 36736691/4988742 और मुझे बताएं कि क्या यह हल करता है। यदि आप इसे पहले ही हल कर चुके हैं तो कृपया अपना समाधान साझा करें। –

उत्तर

1

यह संबंधित समय समाप्त हो सकता है। कॉल करने के लिए timeout पैरा (सेकेंड में) जोड़ने का प्रयास करें: exec_command(timeout=20*60)। यह 20 मिनट का उदाहरण है। अधिक जानकारी के लिए उस विधि से डॉक स्ट्रिंग देखें: https://github.com/paramiko/paramiko/issues/109

में https://github.com/paramiko/paramiko/issues/109#issuecomment-111621658

मैं भी इस मुद्दे का अनुभव मेरे सुझाव का प्रयास करें:

def exec_command(self, command, bufsize=-1, timeout=None, get_pty=False): 
    """ 
    Execute a command on the SSH server. A new `.Channel` is opened and 
    the requested command is executed. The command's input and output 
    streams are returned as Python ``file``-like objects representing 
    stdin, stdout, and stderr. 

    :param str command: the command to execute 
    :param int bufsize: 
     interpreted the same way as by the built-in ``file()`` function in 
     Python 
    :param int timeout: 
     set command's channel timeout. See `Channel.settimeout`.settimeout 
    :return: 
     the stdin, stdout, and stderr of the executing command, as a 
     3-tuple 

    :raises SSHException: if the server fails to execute the command 
    """ 

इसके अलावा वहाँ एक और मुद्दा है कि मैं अनुभव जो भी योगदान कर सकता है यह == stdout.channel.eof_received को 0

import paramiko 
client = paramiko.SSHClient() 
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
client.connect("1.1.1.1", username="root", password="pass") 
stdin, stdout, stderr = client.exec_command("service XXX start") 

stdin वजह से है, stdout और stderr खुला रह रहे हैं ...

>>> print stdin 
<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>> 
>>> print stdout 
<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>> 
>>> print stderr 
<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>> 

तो EOF से नहीं लिया गया ...

>>> print stdin.channel.eof_received 
0 

आमतौर पर मैं यह सच है प्राप्त करते हैं और कर सकते हैं बस stdout.read(), लेकिन सुरक्षित मैं इस का उपयोग होने के लिए वैकल्पिक हल (जो काम करता है!): समय समाप्त के लिए प्रतीक्षा करें, stdout.channel.close (मजबूर) और फिर stdout.read(): http://stackoverflow.com/a/:

>>> timeout = 30 
>>> import time 
>>> endtime = time.time() + timeout 
>>> while not stdout.channel.eof_received: 
...  sleep(1) 
...  if time.time() > endtime: 
...   stdout.channel.close() 
...   break 
>>> stdout.read() 
'Starting XXX: \n[ OK ]\rProgram started . . .\n' 
>>> 
संबंधित मुद्दे