Revision 2d6cfa31 lib/utils.py

b/lib/utils.py
815 815
    raise
816 816

  
817 817

  
818
def _SplitKnownHostsHosts(hosts):
819
  """Parses the first field of a known_hosts file.
820

  
821
  TODO: Support other formats.
822
  """
823
  return hosts.split(',')
824

  
825

  
826
def AddKnownHost(file_name, hostname, pubkey):
827
  """Adds a new known host to a known_hosts file.
828

  
829
  """
830
  f = open(file_name, 'a+')
831
  try:
832
    nl = True
833
    for line in f:
834
      fields = line.split()
835
      if (len(fields) < 3 or
836
          fields[0].startswith('#') or
837
          fields[1] != 'ssh-rsa'):
838
        continue
839
      hosts = _SplitKnownHostsHosts(fields[0])
840
      if hostname in hosts and fields[2] == pubkey:
841
        break
842
      nl = line.endswith('\n')
843
    else:
844
      if not nl:
845
        f.write("\n")
846
      f.write(hostname)
847
      f.write(' ssh-rsa ')
848
      f.write(pubkey)
849
      f.write("\n")
850
      f.flush()
851
  finally:
852
    f.close()
853

  
854

  
855
def RemoveKnownHost(file_name, hostname):
856
  pass
857

  
858

  
818 859
def CreateBackup(file_name):
819 860
  """Creates a backup of a file.
820 861

  

Also available in: Unified diff