Package paramiko :: Module config
[frames] | no frames]

Source Code for Module paramiko.config

  1  # Copyright (C) 2006-2007  Robey Pointer <robey@lag.net> 
  2  # 
  3  # This file is part of paramiko. 
  4  # 
  5  # Paramiko is free software; you can redistribute it and/or modify it under the 
  6  # terms of the GNU Lesser General Public License as published by the Free 
  7  # Software Foundation; either version 2.1 of the License, or (at your option) 
  8  # any later version. 
  9  # 
 10  # Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY 
 11  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 12  # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more 
 13  # details. 
 14  # 
 15  # You should have received a copy of the GNU Lesser General Public License 
 16  # along with Paramiko; if not, write to the Free Software Foundation, Inc., 
 17  # 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA. 
 18   
 19  """ 
 20  L{SSHConfig}. 
 21  """ 
 22   
 23  import fnmatch 
 24   
 25   
26 -class SSHConfig (object):
27 """ 28 Representation of config information as stored in the format used by 29 OpenSSH. Queries can be made via L{lookup}. The format is described in 30 OpenSSH's C{ssh_config} man page. This class is provided primarily as a 31 convenience to posix users (since the OpenSSH format is a de-facto 32 standard on posix) but should work fine on Windows too. 33 34 @since: 1.6 35 """ 36
37 - def __init__(self):
38 """ 39 Create a new OpenSSH config object. 40 """ 41 self._config = [ { 'host': '*' } ]
42
43 - def parse(self, file_obj):
44 """ 45 Read an OpenSSH config from the given file object. 46 47 @param file_obj: a file-like object to read the config file from 48 @type file_obj: file 49 """ 50 config = self._config[0] 51 for line in file_obj: 52 line = line.rstrip('\n').lstrip() 53 if (line == '') or (line[0] == '#'): 54 continue 55 if '=' in line: 56 key, value = line.split('=', 1) 57 key = key.strip().lower() 58 else: 59 # find first whitespace, and split there 60 i = 0 61 while (i < len(line)) and not line[i].isspace(): 62 i += 1 63 if i == len(line): 64 raise Exception('Unparsable line: %r' % line) 65 key = line[:i].lower() 66 value = line[i:].lstrip() 67 68 if key == 'host': 69 # do we have a pre-existing host config to append to? 70 matches = [c for c in self._config if c['host'] == value] 71 if len(matches) > 0: 72 config = matches[0] 73 else: 74 config = { 'host': value } 75 self._config.append(config) 76 else: 77 config[key] = value
78
79 - def lookup(self, hostname):
80 """ 81 Return a dict of config options for a given hostname. 82 83 The host-matching rules of OpenSSH's C{ssh_config} man page are used, 84 which means that all configuration options from matching host 85 specifications are merged, with more specific hostmasks taking 86 precedence. In other words, if C{"Port"} is set under C{"Host *"} 87 and also C{"Host *.example.com"}, and the lookup is for 88 C{"ssh.example.com"}, then the port entry for C{"Host *.example.com"} 89 will win out. 90 91 The keys in the returned dict are all normalized to lowercase (look for 92 C{"port"}, not C{"Port"}. No other processing is done to the keys or 93 values. 94 95 @param hostname: the hostname to lookup 96 @type hostname: str 97 """ 98 matches = [x for x in self._config if fnmatch.fnmatch(hostname, x['host'])] 99 # sort in order of shortest match (usually '*') to longest 100 matches.sort(lambda x,y: cmp(len(x['host']), len(y['host']))) 101 ret = {} 102 for m in matches: 103 ret.update(m) 104 del ret['host'] 105 return ret
106