[工具] Shell 批量实现其他多台电脑可以免密码 SSH 本电脑

介绍

基本信息

作者:朱明宇
名称:批量实现其他多台电脑可以免密码 SSH 本电脑
作用:批量将其他多台电脑的公钥拷贝给本电脑

使用方法

1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本

脚本分割线里的变量

1. nm=192.168.4.0 #网段,网段必须为 C 类网段,请保证格式和前 3 个网络位一定正确
2. sip=51 #起始 ip,起始 IP 地址,IP 地址 的范围是 0-255
3. lip=51 #结束 ip ,结束 IP 地址,IP 地址 的范围是 0-255
4. pd=123456 #其他服务器的 root 远程登录密码
5. lpd=Taren1 #本机的 root 远程登录密码
6. lh=192.168.4.254 #本机 IP 地址

注意

1. 此脚本执行前必须要先保证执行本脚本的用户能无密码 ssh 远程这些远程服务器
2. 必须以 root 用户执行本脚本

脚本

#!/bin/bash

####################### Separator ########################

nm=192.168.4.0 #Network segment. The network segment must be a class C network segment. Please ensure that the format and the first three network bits are correct
sip=51 #Starting IP, starting IP address. The range of IP address is 0-255
lip=51 #End IP, end IP address. The range of IP address is 0-255
pd=123456 #Root remote login password of other servers
lpd=Taren1 #Root remote login password of this machine
lh=192.168.4.254 #Native IP address

####################### Separator ########################

fnm=${nm%.*}

set timeout 3
rpm -q expect

if [ $? -ne 0 ];then
        yum -y install expect &> /dev/null
fi

for i in `seq $sip $lip`
do
        expect << EOF
        spawn ssh root@$fnm.$i
        expect "(yes/no)?"                               {send "yes\r" } 
        expect "password:"                               {send "$pd\r" } 
        expect "#"                                       {send "ssh-keygen\r"}
        expect "(/root/.ssh/id_rsa):"                    {send "\r"}
        expect "(empty for no passphrase):"              {send "\r"}
        expect "passphrase again:"                       {send "\r"}
        expect "#"                                       {send "\r"}
        EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "password:"                               {send "$pd\r" } 
expect "#"                                       {send "ssh-keygen\r"}
expect "(/root/.ssh/id_rsa):"                    {send "\r"}
expect "(empty for no passphrase):"              {send "\r"}
expect "passphrase again:"                       {send "\r"}
expect "#"                                       {send "\r"}
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "(yes/no)?"                               {send "yes\r" } 
expect "#"                                       {send "ssh-keygen\r"}
expect "(/root/.ssh/id_rsa):"                    {send "\r"}
expect "(empty for no passphrase):"              {send "\r"}
expect "passphrase again:"                       {send "\r"}
expect "#"                                       {send "\r"}
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "#"                                       {send "ssh-keygen\r"}
expect "(/root/.ssh/id_rsa):"                    {send "\r"}
expect "(empty for no passphrase):"              {send "\r"}
expect "passphrase again:"                       {send "\r"}
expect "#"                                       {send "\r"}
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "password:"                      { send "$pd\r" } 
expect "#"                              { send "ssh-copy-id -i ~/.ssh/id_rsa.pub root@$lh\r" }
expect "(yes/no)?"                      { send "yes\r" } 
expect "root@$lh's password:"           { send "$lpd\r" }
expect "#"                              { send "exit\r" }
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "password:"                      { send "$pd\r" } 
expect "#"                              { send "ssh-copy-id -i ~/.ssh/id_rsa.pub root@$lh\r" }
expect "root@$lh's password:"           { send "$lpd\r" }
expect "#"                              { send "exit\r" }
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "#"                              { send "ssh-copy-id -i ~/.ssh/id_rsa.pub root@$lh\r" }
expect "(yes/no)?"                      { send "yes\r" } 
expect "root@$lh's password:"           { send "$lpd\r" }
expect "#"                              { send "exit\r" }
EOF

expect << EOF
spawn ssh root@$fnm.$i
expect "#"                              { send "ssh-copy-id -i ~/.ssh/id_rsa.pub root@$lh\r" }
expect "root@$lh's password:"           { send "$lpd\r" }
expect "#"                              { send "exit\r" }
EOF

echo "$fnm.$i has been finished"

done

[工具] Shell 批量实现本电脑免密码 SSH 多个服务器

介绍

基本信息

作者:朱明宇
名称:批量实现本电脑免密码 SSH 多个服务器
作用:将主本机的 SSH 公钥批量拷贝给其他多个远程服务器

使用方法

1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本

脚本分割线里的变量

1. nm=192.168.4.0 #网段,网段必须为 C 类网段,请保证格式和前 3 个网络位一定正确
2. sip=50 #起始 IP 地址,IP 地址 的范围是 0-255
3. lip=57 #结束 IP 地址,IP 地址 的范围是 0-255
4. pd=123456 #其他服务器的 root 远程登录密码

脚本

#!/bin/bash

####################### Separator ########################

nm=192.168.4.0
sip=50
lip=57
pd=123456

####################### Separator ########################

fnm=${nm%.*}

set timeout 3
rpm -q expect

if [ $? -ne 0 ];then
        yum -y install expect &> /dev/null
fi

cat /root/.ssh/id_rsa &> /dev/null

if [ $? -ne 0 ];then

        expect << EOF
        spawn ssh-keygen
        expect "(/root/.ssh/id_rsa):"                    {send "\r"}
        expect "(empty for no passphrase):"              {send "\r"}
        expect "passphrase again:"                       {send "\r"}
        expect "#"                                       {send "\r"}
        EOF

fi

for i in `seq $sip $lip`
do
        echo $fnm.$i
        ping -c3 -i0.3 -w1 $fnm.$i &> /dev/null
        if [ $? -ne 0 ];then
                echo "$fnm.$i can't be connected"
                continue
        fi

        expect << EOF
        spawn ssh-copy-id $fnm.$i
        expect "? "                                      {send "yes\r"}
        expect "password:"                               {send "$pd\r"}
        expect "#"                                       {send "\r"}
        EOF

        expect << EOF
        spawn ssh-copy-id $fnm.$i
        expect "password:"                               {send "$pd\r"}
        expect "#"                                       {send "\r"}
        EOF

        echo "$fnm.$i has been finished"
done

[工具] Shell 批量修改多个 MySQL root 的初始密码

介绍

基本信息

作者:朱明宇
名称:批量修改多个 MySQL root 的初始密码
作用:批量修改多个 MySQL root 的初始密码

使用方法

1. 确认 MariaDB&MySQL 已提前装好
2. 在此脚本的分割线内写入相应的内容
3. 给此脚本添加执行权限
4. 执行此脚本

脚本分割线里的变量

1. nm=192.168.4.0 #网段,网段必须为 C 类网段,请保证格式和前三个网络位一定正确
2. sip=51 #起始 ip,ip 的范围是 0-255
3. lip=53 #结束 ip,ip 的范围是 0-255
4. pd=123456 #想要修改的 MySQL 密码,至少必须要 6 个数

注意

此脚本执行前必须要先保证执行本脚本的用户能无密码 ssh 远程这些远程服务器

脚本

#!/bin/bash

####################### Separator ########################

nm=192.168.4.0
sip=51
lip=53
pd=123456

####################### Separator ########################

fnm=${nm%.*}

set timeout 3

rpm -q expect

if [ $? -ne 0 ];then
        yum -y install expect &> /dev/null
fi

for i in `seq $sip $lip`
do

        echo $fnm.$i

        ssh $fnm.$i 'grep validate_password_policy=0 /etc/my.cnf'
        if [ $? -ne 0 ];then
                ssh $fnm.$i 'sed -i "/^\[mysqld\]$/a validate_password_policy=0" /etc/my.cnf'
        fi

        ssh $fnm.$i 'grep validate_password_length=6 /etc/my.cnf'
        if [ $? -ne 0 ];then
                ssh $fnm.$i 'sed -i "/^\[mysqld\]$/a validate_password_length=6" /etc/my.cnf'
        fi

        ssh $fnm.$i 'systemctl restart mysqld'

        a=`ssh $fnm.$i "grep 'password is generated for root@localhost' /var/log/mysqld.log" |tail -1 | awk '{print $NF}'`

        expect << EOF
        spawn ssh $fnm.$i "mysqladmin -uroot -p password $pd "
        expect "Enter password:"            {send "$a\r"}
        expect "#"                          {send "\r"}
        EOF

done

[工具] Shell 批量实现多个远程服务器执行本电脑上的一个脚本

介绍:

基本信息

作者:朱明宇
名称:批量实现多个远程服务器执行本主机的一个脚本
作用:将本主机的某个脚本批量拷贝到多个远程服务器并执行

使用方法

1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本

脚本分割线里的变量

1. nm=192.168.4.0 #网段,网段必须为 C 类网段,请保证格式和前 3 个网络位一定正确
2. sip=50 #起始 IP 地址,IP 地址的范围是 0-255
3. lip=57 #结束 IP 地址,IP 地址的范围是 0-255
4. pd=123456 #其他服务器的 root 远程登录密码
5. fn=’ssh-copy-id.sh’ #要远程执行的脚本的名称

注意

此脚本执行前必须要先保证执行本脚本的用户能无密码 ssh 远程这些远程服务器

脚本:

#!/bin/bash

####################### Separator ########################

nm=192.168.4.0
sip=50
lip=57
pd=123456
fn='ssh-copy-id.sh'

####################### Separator ########################

fnm=${nm%.*}

for i in `seq $sip $lip`
do
        ping -c3 -i0.3 -w1 $fnm.$i &> /dev/null

        if [ $? -ne 0 ];then 
                echo "$fnm.$i can't be connected"
        else
                scp $fn $fnm.$i:/root/
                ssh $fnm.$i "chmod +x /root/$fn"
                ssh $fnm.$i "bash /root/$fn"
                ssh $fnm.$i "rm -rf /root/$fn"
                echo "$fn has been executed on $fnm.$i"
        fi
done

[娱乐] Shell 随机点名

介绍

基本信息

作者:朱明宇
名称:随机点名
作用:在一列名单中随机选出一个名字

使用方法

1. 将此脚本和清单 $list 文件放在同一目录下
2. 清单 $list 里每一个名字占用一行
3. 给此脚本添加执行权限
4. 执行此脚本

脚本分割线里的变量

1. list=”list.txt” #解释:指定清单的目录和名称
2. explain=”Congratulations to the winner: ” #解释:自定义最终显示结果的解释

补充

清单示例如下,可直接复制粘贴,如果不更改脚本,清单请命名为 list.txt:

圣女贞德
叶卡捷玲娜
伊莎贝拉
武则天
克利奥帕特拉

脚本

#!/bin/bash

####################### Separator ########################

list="list.txt"
explain="Congratulations to the winner: "

####################### Separator ########################

read -p "In order to achieve random visual effect, it is suggested that the number of random times should be more than 100 ? " n
i=1
a=`cat $list | wc -l`

while [ $i -le $n ]
do
        r=$[RANDOM%$[a]]
        let r++
        echo -e "\033[2J\033[10A\033[34m`sed -n "$[r]p" list.txt`\c"
        let i++
        echo
        sleep 0.001
done

echo -e "\033[36m$explain\033[35m`sed -n "$[r]p" list.txt`\033[0m "
echo