Tuesday, February 9, 2016

script - copy public key to remote server


#!/bin/bash
# Tue Aug 4 09:14:09 EDT 2015
# generate your key using ssh-keygen -t rsa -N "" -b 2048
# Copy authorized key to remove host
# Sam Bhusal
# OS: Solaris 10, should also work on Linux env
# copy_pkey.sh

# using for to loop through, you may find better option
for i in `cat ../etc/hosts.ip | grep -v "#" | awk '{print $1}'`
do
echo "Checking $i"
ssh-keyscan $i >> ~/.ssh/known_hosts
ssh -q $i mkdir .ssh
scp -qp authorized_keys $i:/export/home/sam/.ssh
#ssh -q $i /usr/local/bin/sudo cat /etc/shadow | egrep "$USERS"
# ssh -q $i cat /etc/passwd | egrep "$USERS"

done
# EOF

# Note:
# ssh-copy-id is not available on Solaris servers.
# ssh-copy-id -i ~/.ssh/id_rsa.pub remote-server
# ssh-copy-id appends the public key on to the remote-server’s .ssh/authorized_key under the users home dir
# Once you done, try logging from your machine toremove "ssh remote server.
# should not prompt you for password.

--------------------------------
$ cd etc
$ cat hosts.ip

192.168.10.200 dnsserv1
192.168.10.130 appserv1
# 192.168.10.20 dnsserv1
192.168.10.210 dnsserv2
192.168.10.140 appserv2


$ sh copy_pkey.sh

No comments:

Post a Comment