Linux Shell脚本自动化编程实战- scp非交互传文件

一、ssh远程登陆

1、认识expect

2、shell ssh自动登陆

 3、实现ssh批量拷贝公钥

代码:

#!/usr/bin/bash
>ip.txt
## 主机密码
password=admin

## 检查是否安装expect
rpm -q expect>&/dev/null

if [ $? -ne 0 ];then
        yum install -y expect
fi

## 生成公钥
if [ ! -f ~/.ssh/id_rsa ];then
        ssh-keygen -P "" -f ~/.ssh/id_rsa
fi

## 循环拷贝公钥
for i in {10..111}
do
        {
        ip=192.168.100.$i
        ping -c1 -W1 $ip &>/dev/null
        if [ $? -eq 0 ];then
                echo "$ip" >> ip.txt
                /usr/bin/expect <<-EOF
        spawn ssh-copy-id $ip
        expect {
            "yes/no" { send "yes\r"; exp_continue }
            "password:" { send "$password\r" }
        }
        expect eof
        EOF
        fi
        }&
done
wait
echo "ssh免密配置完成"
cat ip.txt
个人脚本仓库:shell脚本: shell自动化脚本 (gitee.com)
posted @ 2022-02-09 14:33  人走茶良  阅读(504)  评论(0编辑  收藏  举报
推荐:华为云