0

Greetings I am making a script to automate the sending of public keys but I cannot send my file or variable through ssh either by "echo or cat"

3 Answers 3

1

If you want to send a file to a remote server, you can use scp.

From man scp:

scp copies files between hosts on a network. It uses ssh(1) for data transfer, and uses the same authentication and provides the same security as ssh(1). Unlike rcp(1), scp will ask for passwords or passphrases if they are needed for authentication.

Basic usage:

scp user@origin:/path/to/file user@target:/path/to/file

Assuming you want to transfer example.txt from current directory to a remote SERVER with the same name on a folder called example authenticating as root:

scp example.txt root@SERVER:/example/example.txt
1

For transferring public keys to remote servers, there is a dedicated utility for it ssh-copy-id.

You will find above a script you can use to automate this task :

#!/bin/sh
while read server ; do
   timeout 10 ssh-copy-id -i <public_key_path> <user>@$server
done < <list_of_servers.txt>

P.S : Replace the constants between <> by your own values.

0

You can use sshpass in your script to access the server without entering the password every time like:

sshpass -p 'PASSWORD' ssh USER@SERVER ssh-copy-id USER@SERVER

also you can use -f to write the password in a file if you don't want to write it in the command line (it's in clear text in both cases) for a list of servers called servers.list

for SERVER in $(cat servers.list)
do
sshpass -p 'PASSWORD' ssh USER@$SERVER ssh-copy-id USER@$SERVER
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .