Advertisement
Guest User

Untitled

a guest
May 10th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. import getpass
  2. import os
  3. import paramiko
  4.  
  5. def copy_ssh_keys(hosts):
  6. username = raw_input('Username: ')
  7. password = getpass.getpass('Password: ')
  8. with open(os.path.expanduser('~/.ssh/id_rsa.pub')) as f:
  9. ssh_key = f.read()
  10. for host in hosts:
  11. client = paramiko.SSHClient()
  12. client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  13. client.connect(host, username=username, password=password)
  14. client.exec_command('mkdir -p ~/.ssh/')
  15. client.exec_command(
  16. 'grep -q -F "{0}" {1} || '
  17. 'echo "{0}" >> {1}'
  18. ''.format(ssh_key, '~/.ssh/authorized_keys')
  19. )
  20. client.exec_command('chmod 644 ~/.ssh/authorized_keys')
  21. client.exec_command('chmod 700 ~/.ssh/')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement