批量操作主机

批量修改密码

#!/bin/bash
<<bak
old_pass.txt 文件示例
192.168.1.2 root password 22
192.168.1.3 root password 22
bak

[ -f /etc/init.d/functions ] && . /etc/init.d/functions
OLD_INFO=old_pass.txt
NEW_INFO=new_pass.txt
# yum install -y sshpass
for IP in $(awk '/^[^#]/{print $1}' $OLD_INFO); do
    USER=$(awk -v I=$IP 'I==$1{print $2}' $OLD_INFO)
    PASS=$(awk -v I=$IP 'I==$1{print $3}' $OLD_INFO)
    PORT=$(awk -v I=$IP 'I==$1{print $4}' $OLD_INFO)
    NEW_PASS=$(openssl rand -base64 8)
    #NEW_PASS=1
    sshpass -p "$PASS" ssh -p $PORT $USER@$IP -o StrictHostKeyChecking=no "echo $NEW_PASS |passwd --stdin $USER" && \
    (
    echo "$IP $USER $NEW_PASS $PORT [`date '+%F %T'`]" >> $NEW_INFO && \
    action "配置成功 $USER $IP $NEW_PASS" /bin/true
    ) || \
    action "配置失败 $USER $IP $PASS" /bin/false
done

批量传递秘钥

#!/bin/bash
<<bak
pass.txt 文件示例
192.168.1.2 root password 22
192.168.1.3 root password 22
bak
[ ! -f $HOME/.ssh/id_rsa ] && ssh-keygen -t rsa -b 2048 -N "" -f $HOME/.ssh/id_rsa
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
INFO=pass.txt
# yum install -y sshpass
for IP in $(awk '/^[^#]/{print $1}' $INFO); do
    USER=$(awk -v I=$IP 'I==$1{print $2}' $INFO)
    PASS=$(awk -v I=$IP 'I==$1{print $3}' $INFO)
    PORT=$(awk -v I=$IP 'I==$1{print $4}' $INFO)
    sshpass -p $PASS ssh-copy-id -o StrictHostKeyChecking=no ${USER}@${IP}  && \
    action "秘钥传输成功 $IP" /bin/true || \
    action "秘钥传输失败 $IP" /bin/false
done

批量拷贝文件

#!/bin/bash
<<bak
pass.txt 文件示例
192.168.1.2 root password 22
192.168.1.3 root password 22
bak
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
INFO=pass.txt
# yum install -y sshpass
for IP in $(awk '/^[^#]/{print $1}' $INFO); do
    USER=$(awk -v I=$IP 'I==$1{print $2}' $INFO)
    PASS=$(awk -v I=$IP 'I==$1{print $3}' $INFO)
    PORT=$(awk -v I=$IP 'I==$1{print $4}' $INFO)
    sshpass -p $PASS scp -o StrictHostKeyChecking=no $1 ${USER}@${IP}:$2  && \
    action "拷贝成功 $IP" /bin/true || \
    action "拷贝失败 $IP" /bin/false
done