• +43 660 1453541
  • contact@germaniumhq.com

How to Automatically Upload Public Ssh Keys to Hosts

How to Automatically Upload Public Ssh Keys to Hosts

Whenever a new server needs to be managed, the first task is to upload my public key on that system, so I don’t need to ever remember the password again, and use it again. Here’s how I automated that process.

First it seems kind of an obvious process. First we need to pass in the credentials to the system, and connect to it, then perform the upload. In order to achieve that, let’s create a small adhesive process. First, in an user task we ask for the credentials:

@adhesive.usertask("SSH Credentials")
def ssh_credentials(context, ui):
    ui.add_input_text("ssh_host", title="Host", value="localhost")
    ui.add_input_text("ssh_port", title="Port", value="22")
    ui.add_input_text("ssh_user", title="User")
    ui.add_input_password("ssh_pass", title="Pass")

When executing this task, we’ll get the following nice dialog to fill in:

SSH User Task

Then using the filled in data, in a regular task, we perform the actual upload:

@adhesive.task('Copy the id_rsa.pub over there')
def copy_idrsa_pub(context):
    content = context.workspace.run("""
        cat /home/raptor/.ssh/id_rsa.pub
    """, capture_stdout=True)

    with ssh.inside(
        ) as ssh_workspace:

        temp_file_name = f"/tmp/{str(uuid.uuid4())}"
            ssh_workspace.write_file(temp_file_name, content)
                cd $HOME
                mkdir -p .ssh
                cd .ssh
                chmod 711 .
                cat {temp_file_name} >> authorized_keys
                chmod 600 authorized_keys

Again, nothing super interesting, except maybe that we use both write_file and run from the ssh connection, so we don’t hardcode the $HOME of the user.

We simply execute it, and we’re done:


To try it out you need a Python 3.6+, and adhesive installed (pip install adhesive)