Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import getpass
- import os
- import paramiko
- def copy_ssh_keys(hosts):
- username = raw_input('Username: ')
- password = getpass.getpass('Password: ')
- with open(os.path.expanduser('~/.ssh/id_rsa.pub')) as f:
- ssh_key = f.read()
- for host in hosts:
- client = paramiko.SSHClient()
- client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- client.connect(host, username=username, password=password)
- client.exec_command('mkdir -p ~/.ssh/')
- client.exec_command(
- 'grep -q -F "{0}" {1} || '
- 'echo "{0}" >> {1}'
- ''.format(ssh_key, '~/.ssh/authorized_keys')
- )
- client.exec_command('chmod 644 ~/.ssh/authorized_keys')
- client.exec_command('chmod 700 ~/.ssh/')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement