Advertisement
AdmiralNemo

Find SSH Keys

Sep 14th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. '''
  2. Created on Jul 1, 2010
  3.  
  4. @author: dustin
  5. '''
  6. from paramiko import * #@UnusedWildImport
  7. import os
  8. import platform
  9.  
  10. class SSHClient(SSHClient):
  11.     def load_user_host_keys(self, filename=None):
  12.         '''Loads a user key file and returns its filename.'''
  13.        
  14.         if not filename:
  15.             #Look for the user's key file in various places
  16.             if platform.system() == 'Windows' and os.path.exists("%s/ssh/known_hosts" % os.getenv("APPDATA")):
  17.                 # %APPDATA%\ssh\known_hosts on Windows
  18.                 filename = "%s/ssh/known_hosts" % os.getenv("APPDATA")
  19.             elif os.path.exists(os.path.expanduser('~/.ssh/known_hosts')):
  20.                 # ~/.ssh/known_hosts, if it exists
  21.                 filename = os.path.expanduser('~/.ssh/known_hosts')
  22.             elif os.path.exists(os.path.expanduser('~/ssh/known_hosts')):
  23.                 # ~/ssh/known_hosts, common on Windows
  24.                 filename = os.path.expanduser('~/ssh/known_hosts')
  25.        
  26.         #Load the key file, if it was found
  27.         if filename:
  28.             self.load_host_keys(filename)
  29.         #No key file was found, attempt to create one
  30.         else:
  31.             try:
  32.                 if platform.system() == 'Windows':
  33.                     if not os.path.exists("%s/ssh" % os.getenv("APPDATA")):
  34.                         os.mkdir("%s/ssh" % os.getenv("APPDATA"))
  35.                     filename = "%s/ssh/known_hosts" % os.getenv("APPDATA")
  36.                 else:
  37.                     if not os.path.exists(os.path.expanduser("~/.ssh")):
  38.                         os.mkdir(os.path.expanduser("~/.ssh"))
  39.                     filename = os.path.expanduser("~/.ssh/known_hosts")
  40.             except (IOError, OSError):
  41.                 pass
  42.  
  43.         return filename
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement