Today I saw a great God's SSH Key distribution script, in which he had a ssh-copy-id-o stricthostkeychecking=no-i ~/.ssh/id_rsa.pub root@ $ip in his shell script, and after I executed, I reported/ Usr/bin/ssh-copy-id:error:no identities found, and then on the Internet to find the next-o stricthostkeychecking=no the meaning of the original is mandatory do not check key, But Ssh-copy-id I can not use here, so in the end I can only remove-o stricthostkeychecking=no.
Original script:
The code is as follows |
Copy Code |
#!/bin/sh Read-p "Enter remote server IP:" IP Ssh-copy-id-o stricthostkeychecking=no-i ~/.ssh/id_rsa.pub root@ $ip SSH root@ $ip ' sed-i ' s/^ #RSAAuthentication yes/rsaauthentication yes/g '/etc/ssh/sshd_config ' SSH root@ $ip ' sed-i ' s/^ #PubkeyAuthentication yes/pubkeyauthentication yes/g '/etc/ssh/sshd_config ' SSH root@ $ip ' sed-i ' s/^ #PermitRootLogin yes/permitrootlogin yes/g '/etc/ssh/sshd_config ' SSH root@ $ip ' service sshd restart ' Hostname= ' ssh root@${ip} ' hostname ' echo "Add host name and IP to local/etc/hosts file" echo "$ip $hostname" >>/etc/hosts echo "Remote host host name is $hostname, see/etc/hosts Ensure that the hostname and IP are added to the host list file" |
Modified script:
code is as follows |
copy code |
#!/bin/sh Read-p" Enter remote server IP: "IP ssh-copy-id-i ~/.ssh/id_rsa.pub $ip ssh root@ $ip ' sed-i ' s/^ #RSAAuthentication yes/rsaauthentication yes/g '/etc/ssh/sshd_config ' ssh root@ $ip ' Sed-i "s/^ #PubkeyAuthentication yes/pubkeyauthentication yes/g"/etc/ssh/sshd_config " ssh root@ $ip ' sed-i ' s/^# Permitrootlogin yes/permitrootlogin yes/g "/etc/ssh/sshd_config" ssh root@ $ip ' service sshd restart ' hostname= ' SSH root@${ip} ' hostname ' Echo Add host name and IP to local/etc/hosts file echo "$ip $hostname" >>/etc /hosts Echo Remote host host name is $hostname, see/etc/hosts Ensure that host name and IP are added to host list file " |
In this way, we will not report/usr/bin/ssh-copy-id:error:no identities found error.
SSH Key Distribution Script
the first is to generate the SSH key script
The code is as follows |
Copy Code |
#!/bin/sh Ssh-keygen-t Rsa-p '-F ~/.ssh/id_rsa CP ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys |
Ssh-keygen generally need to input passphrase, but generally are three return to the past, I lazy input, plus-P "" will not.
Then add the public key to the script from the node
code is as follows |
copy code |
#!/bin/sh read-p" Enter remote server IP: "IP Ssh-copy-id-o stricthostkeychecking=no- I ~/.ssh/id_rsa.pub root@ $ip ssh root@ $ip ' sed-i s/^ #RSAAuthentication yes/rsaauthentication yes/g '/etc/ssh/sshd_ Config ' ssh root@ $ip ' sed-i ' s/^ #PubkeyAuthentication yes/pubkeyauthentication yes/g '/etc/ssh/sshd_config ' SSH root@ $ip ' sed-i ' s/^ #PermitRootLogin yes/permitrootlogin yes/g '/etc/ssh/sshd_config ' ssh root@ $ip ' service sshd Restart ' hostname= ' ssh root@${ip} ' hostname ' Echo Add host name and IP to local/etc/hosts file " echo" $ip $hostname ">>/etc/hosts echo" Remote host host name is $hostname, see/etc/hosts Ensure that host name and IP are added to host list file " Echo host Public key replication Complete" |
Then the third script reads the host list and then copies the/etc/hosts to all hosts.
The code is as follows |
Copy Code |
#!/bin/sh cat/etc/hosts | While Read line Todo Ip= ' echo $LINE | awk ' {print $} ' | Grep-v "::" | Grep-v "127.0.0.1" echo "Copying/etc/hosts to ${ip}" Scp-o stricthostkeychecking=no/etc/hosts root@${ip}:/etc/ Done |
Reference:
Http://blog.slogra.com and 51cto.com