Copy certificate from master to slaves
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.5 KiB

7 years ago
7 years ago
  1. #!/usr/bin/python
  2. import paramiko
  3. import argparse
  4. import os
  5. from subprocess import list2cmdline
  6. def connection_from_config(name):
  7. """Create a connection from a Host entry in the .ssh/config file"""
  8. config = paramiko.config.SSHConfig()
  9. cp = os.path.expanduser('~/.ssh/config')
  10. with open(cp) as file:
  11. config.parse(file)
  12. info = config.lookup(name)
  13. info['username'] = info.pop('user')
  14. info['key_filename'] = info.pop('identityfile')
  15. info['port'] = int(info['port'])
  16. client = paramiko.client.SSHClient()
  17. client.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
  18. client.connect(**info)
  19. return client
  20. def scp(connection: paramiko.sftp_client.SFTPClient, src, dst):
  21. """Function to copy files from src to dst using connection"""
  22. connection.put(src, dst)
  23. def exec_remote(connection: paramiko.SSHClient, command):
  24. """Function to execute specified command on conection"""
  25. if not isinstance(command, str):
  26. command = list2cmdline(command)
  27. stdin, stdout, stderr = connection.exec_command(command)
  28. return stdout, stderr
  29. if __name__ == "__main__":
  30. parser = argparse.ArgumentParser()
  31. parser.add_argument(
  32. 'cert_path', help="location of the certificate to install")
  33. parser.add_argument(
  34. 'key_path', help="localtion of the private key to install")
  35. parser.add_argument(
  36. 'store_path',
  37. help="directory to store the private key and certificate")
  38. parser.add_argument('-d', '--dry', action='store_true')
  39. args = parser.parse_args()