2011-12-24 6 views
6

का उपयोग करके मैंने पैरामीको के माध्यम से एक एसएसएच कनेक्शन बनाने के लिए एक सरल कार्यक्रम लिखा और फिर एक साधारण कमांड निष्पादित किया। लेकिन यह हमेशा एक अपवाद त्रुटि फेंकता है: -गॉट अपवाद त्रुटि "थ्रेड थ्रेड -1 में अपवाद (सबसे अधिक संभावना दुभाषिया शटडाउन के दौरान उठाई गई)" जो पैरामीको

Exception in thread Thread-1 (most likely raised during interpreter shutdown): Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 530, in __bootstrap_inner
File "/usr/lib/python2.7/site-packages/paramiko/transport.py", line 1574, in run : 'NoneType' object has no attribute 'error'

प्रोग्राम है जो मैंने लिखा इस प्रकार है: -

class Session: 

     def __init__(self, ipaddr, username, password): 
     self.ipaddr = ipaddr 
     self.username = username 
     self.password = password 

     self.connect() 

     def connect(self): 
     try: 
      time.sleep(1) 
      self.ssh = paramiko.SSHClient() 
      self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 

      try: 
      self.ssh.connect(self.ipaddr, username=self.username, password=self.password) 
      time.sleep(2) 
      except socket.error, e: 
      print e 
      self.ssh.close() 
      sys.exit() 

     except Exception, e: 
      print e 

    def executeCmd(self, cmd): 
    data = "" 
    try: 
     stdin, stdout, stderr = self.ssh.exec_command(cmd) 
     data = stdout.read() 
    except SSHException, e: 
     print "Error: ", e 
     errorMsg = "Error: %s" %traceback.format_exc() 
     print errorMsg 

    return data 

    def __del__(self): 
    self.ssh.close() 

कैसे इस अपवाद को हल करने? कृपया मदद करे।

धन्यवाद

+1

लगता है जैसे यह इस सवाल का डुप्लिकेट है: http://stackoverflow.com/questions/1745232/solving-thread-cleanup-on-paramiko लेकिन इस सवाल का शीर्षक लॉन्चपैड में बेहतर – ifischer

+0

संबंधित बग है (नया और अप्रयुक्त) यहां: https://bugs.launchpad.net/paramiko/+bug/786808 – ifischer

+0

पायथन ट्रैकर में संबंधित बग: http://bugs.python.org/issue1722344, पैरामीको-गीथब में संबंधित बग: https : //github.com/paramiko/paramiko/issues/17 – ifischer

उत्तर

1

यहाँ एक नमूना कोड मैंने पाया और पहले का इस्तेमाल किया है और यह मेरे लिए ठीक लग रहा है।

import os 
import tempfile 
import paramiko 

class Connection(object): 
     """Connects and logs into the specified hostname. 
     Arguments that are not given are guessed from the environment.""" 

     def __init__(self, 
     host, 
     username = None, 
     private_key = None, 
     password = None, 
     port = 22, 
     ): 
       self._sftp_live = False 
       self._sftp = None 
       if not username: 
         username = os.environ['LOGNAME'] 

       # Log to a temporary file. 
       templog = tempfile.mkstemp('.txt', 'ssh-')[1] 
       paramiko.util.log_to_file(templog) 

       # Begin the SSH transport. 
       self._transport = paramiko.Transport((host, port)) 
       self._tranport_live = True 
       # Authenticate the transport. 
       if password: 
       # Using Password. 
         self._transport.connect(username = username, password = password) 
       else: 
       # Use Private Key. 
         if not private_key: 
         # Try to use default key. 
           if os.path.exists(os.path.expanduser('~/.ssh/id_rsa')): 
             private_key = '~/.ssh/id_rsa' 
           elif os.path.exists(os.path.expanduser('~/.ssh/id_dsa')): 
             private_key = '~/.ssh/id_dsa' 
         else: 
           raise TypeError, "You have not specified a password or key." 
         private_key_file = os.path.expanduser(private_key) 
         rsa_key = paramiko.RSAKey.from_private_key_file(private_key_file) 
         self._transport.connect(username = username, pkey = rsa_key) 

     def _sftp_connect(self): 
       """Establish the SFTP connection.""" 
       if not self._sftp_live: 
         self._sftp = paramiko.SFTPClient.from_transport(self._transport) 
         self._sftp_live = True 
     def get(self, remotepath, localpath = None): 
       """Copies a file between the remote host and the local host.""" 
       if not localpath: 
         localpath = os.path.split(remotepath)[1] 
       self._sftp_connect() 
       self._sftp.get(remotepath, localpath) 

     def put(self, localpath, remotepath = None): 
       """Copies a file between the local host and the remote host.""" 
       if not remotepath: 
         remotepath = os.path.split(localpath)[1] 
       self._sftp_connect() 
       self._sftp.put(localpath, remotepath) 

     def execute(self, command): 
       """Execute the given commands on a remote machine.""" 
       channel = self._transport.open_session() 
       channel.exec_command(command) 
       output = channel.makefile('rb', -1).readlines() 
       if output: 
         return output 
       else: 
         return channel.makefile_stderr('rb', -1).readlines() 
     def close(self): 
       """Closes the connection and cleans up.""" 
       # Close SFTP Connection. 
       if self._sftp_live: 
         self._sftp.close() 
         self._sftp_live = False 
     # Close the SSH Transport. 
       if self._tranport_live: 
         self._transport.close() 
         self._tranport_live = False 
     def __del__(self): 
       """Attempt to clean up if not explicitly closed.""" 
       self.close() 
संबंधित मुद्दे