ssh批量分发脚本

in 自动化 with 0 comment

通过ssh分发文件。首先要在服务器端生成ssh密钥,把公钥内容发送到客户端/root/.ssh/authorized_keys文件中。
#chmod 700 authorized_keys //设置文件权限为700.其他用户不可写

1.最简单直接的分发脚本
#!/bin/bash ##向Ip尾数为5,6,7的地址分发文件 for n in 5 6 7 do ###--- $1表示第一个参数 scp $1 192.168.213.$n:/dir done

2.增加判断的分发脚本
#!/bin/bash ##向Ip尾数为5,6,7的地址分发文件 if [ $# -ne 1 ] then echo "please ensure $1 is 1" && exit fi for n in 5 6 7 do scp $1 192.168.213.$n:/dir 2>/dev/null x=`echo $?` if [ $x -eq 0 ] then echo "192.168.213.$n is right" else echo "192.168.213.$n is wrong" fi done

3.调用两个参数,即目录+文件
#!/bin/bash ##向Ip尾数为5,6,7的地址分发文件 if [ $# -ne 2 ] then echo "please ensure $1 is 1" && exit fi for n in 5 6 7 do scp $2/$1 192.168.213.$n:/ 2>/dev/null x=`echo $?` if [ $x -eq 0 ] then echo "192.168.213.$n is right" else echo "192.168.213.$n is wrong" fi done

Responses