使用shell脚本ssh实现批量管理

在管理机生成密钥对

ssh-keygen -t rsa 一路回车

去交互式的脚本

#!/usr/bin/expect
set IP [lindex $argv 0]
set PASSWORD 123123
set timeout 10
spawn ssh-copy-id root@$IP
expect {
    "yes/no" {send "yes\r";exp_continue}
    "password:" {send "$PASSWORD\r"}
}
interact

调用上面的脚本实现自动分发密钥

#!/bin/bash
#
yum -y install expect
for I in {151..154}
do
/root/fenfamiyao.sh 192.168.100.$I
done

使用它以下脚本完成小型批量管理分发与执行

#!/bin/bash
#
. /etc/init.d/functions
for I in {151..154}
do
#ssh [email protected].$I $1 &> /dev/null
rsync -zavH $1 [email protected].$I:/root/ &> /dev/null
    if [ $? -eq 0 ]; then
        action "执行192.168.100.$I" /bin/true
    else
        action "执行192.168.100.$I" /bin/false
    fi
done

猜你喜欢

转载自blog.csdn.net/Richardlygo/article/details/81667722