yum -y install expect
yum install sshpass

 

#!/bin/bash
#!/bin/bash
#------------------------------------------#
# FileName: ssh_auto.sh
# Revision: 1.1.0
# Date: 2017-07-14 04:50:33
# Author: vinsent
# Email: hyb_admin@163.com
# Website: www.vinsent.cn
# Description: This script can achieve ssh password-free login,
# and can be deployed in batches, configuration
#------------------------------------------#
# Copyright: 2017 vinsent
# License: GPL 2+
#------------------------------------------#
[ ! -f /root/.ssh/id_rsa.pub ] && ssh-keygen -t rsa -p ‘‘ &>/dev/null # 密钥对不存在则创建密钥
while read line;do
ip=`echo $line | cut -d " " -f1` # 提取文件中的ip
user_name=`echo $line | cut -d " " -f2` # 提取文件中的用户名
pass_word=`echo $line | cut -d " " -f3` # 提取文件中的密码
expect < spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $user_name@$ip
expect {
"yes/no" { send "yes\n";exp_continue}
"password" { send "$pass_word\n"}
}
expect eof
EOF

done < /root/host_ip.txt # 读取存储ip的文件

[root@localhost kubeedge]# ./autossh.sh 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.56.103 # 复制公钥到目标主机
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: ERROR: Too many arguments.  Expecting a target hostname, got: 'root@192.168.56.103' '#' '复制公钥到目标主机'

Usage: /usr/bin/ssh-copy-id [-h|-?|-f|-n] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname
        -f: force mode -- copy keys without trying to check if they are already installed
        -n: dry run    -- no keys are actually copied
        -h|-?: print this help
expect: spawn id exp6 not open
    while executing
"expect eof"
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.56.104 # 复制公钥到目标主机
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: ERROR: Too many arguments.  Expecting a target hostname, got: 'root@192.168.56.104' '#' '复制公钥到目标主机'

Usage: /usr/bin/ssh-copy-id [-h|-?|-f|-n] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname
        -f: force mode -- copy keys without trying to check if they are already installed
        -n: dry run    -- no keys are actually copied
        -h|-?: print this help
expect: spawn id exp6 not open
    while executing
"expect eof"
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.56.105 # 复制公钥到目标主机
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: ERROR: Too many arguments.  Expecting a target hostname, got: 'root@192.168.56.105' '#' '复制公钥到目标主机'

Usage: /usr/bin/ssh-copy-id [-h|-?|-f|-n] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname
        -f: force mode -- copy keys without trying to check if they are already installed
        -n: dry run    -- no keys are actually copied
        -h|-?: print this help
expect: spawn id exp6 not open
    while executing
"expect eof"
[root@localhost kubeedge]# vim ./autossh.sh 
[root@localhost kubeedge]# ./autossh.sh 
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.56.103
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.56.103's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.56.103'"
and check to make sure that only the key(s) you wanted were added.

spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.56.104
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.56.104's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.56.104'"
and check to make sure that only the key(s) you wanted were added.

spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.56.105
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.56.105's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.56.105'"
and check to make sure that only the key(s) you wanted were added.

[root@localhost kubeedge]# ls
autossh.sh  ip.log  ssh.sh
[root@localhost kubeedge]# ssh root@192.168.56.104
Last failed login: Mon Nov  2 06:38:15 EST 2020 from 192.168.56.103 on ssh:notty
There were 3 failed login attempts since the last successful login.
Last login: Mon Nov  2 06:20:29 2020 from 192.168.56.1
[root@localhost ~]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.2.6  netmask 255.255.255.0  broadcast 10.0.2.255
        inet6 fe80::c40f:c6fb:c802:a2ce  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::d475:845d:e391:4321  prefixlen 64  scopeid 0x20<link>
        inet6 fd17:625c:f037:2:a961:3128:f0fa:8abd  prefixlen 64  scopeid 0x0<global>
        ether 08:00:27:ad:18:c9  txqueuelen 1000  (Ethernet)
        RX packets 14839  bytes 19725195 (18.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6564  bytes 472543 (461.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.56.104  netmask 255.255.255.0  broadcast 192.168.56.255
        inet6 fe80::5c24:833a:8dce:c67  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:ba:87:bb  txqueuelen 1000  (Ethernet)
        RX packets 154475  bytes 12247970 (11.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 228511  bytes 25593975 (24.4 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost ~]# 
 

自动化sh脚本实现免密登录_bash