niels68

ssh-copy

Feb 1st, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. import paramiko
  2. import os
  3.  
  4. def copy_ssh_key(user, host, port, key_file):
  5.     key = paramiko.RSAKey(filename=key_file)
  6.     client = paramiko.SSHClient()
  7.     client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  8.  
  9.     try:
  10.         client.connect(hostname=host, port=port, username=user, pkey=key)
  11.         ssh_command = f"echo {key.get_base64()} >> ~/.ssh/authorized_keys"
  12.         stdin, stdout, stderr = client.exec_command(ssh_command)
  13.         print(f"Key copied to {host} successfully.")
  14.     except Exception as e:
  15.         print(f"Failed to copy key to {host}: {str(e)}")
  16.     finally:
  17.         client.close()
  18.  
  19. def main():
  20.     pass
  21.  
  22. if __name__ == "__main__":
  23.     main()
  24.  
Add Comment
Please, Sign In to add comment