shell-第四章循环语句

第四章 Shell循环语句
1. 循环语句for基本概述
2. 循环语句for场景示例
3. 循环语句while基本概述
4. 循环语句while场景示例
5. 内置跳出循环语句指令

  1. 循环语句for基本概述
  2. for循环基础语法

for 变量名 in [ 取值列表 ]
do
循环体
done

  1. for循环基本使用示例

取值列表有多种取值方式,可以直接读取in后面的值,默认以空格做分割符

[root@rlb/scripts]# cat for-1.sh

!/bin/bash

for var in file1 file2 file3
do
echo "The text is $var"
done
[root@rlb /scripts]# sh for-1.sh
The text is file1
The text is file2
The text is file3

  1. for循环基本使用示例,列表中的复杂值,可以使用引号或转义字符""来加以约束

[root@rlb /scripts]# cat for-2.sh

!/bin/bash

for var in file1 "file2 hello" file3
do
echo "The text is $var"
done
[root@rlb/scripts]# sh for-2.sh
The text is file1
The text is file2 hello
The text is file3

转义字符

[root@rlb /scripts]# cat for-3.sh

!/bin/bash

for var in file1 file '2
do
echo "The text is $var"
done
[root@rlb /scripts]# sh for-3.sh
The text is file1
The text is file
The text is '2

  1. for循环基本使用示例,从变量中取值

[root@rlb/scripts]# cat for-4.sh

!/bin/bash

list="file1 file2 file3"
for var in $list
do
echo $var
done
[root@rlb/scripts]# sh for-4.sh
file1
file2
file3

  1. for循环基本使用示例,从命令中取值

[root@rlb /scripts]# cat for-5.sh

!/bin/bash

for var in cat /etc/hosts
do
echo $var
done
[root@rlb/scripts]# sh for-5.sh
127.0.0.1
localhost
localhost.localdomain
localhost4
localhost4.localdomain4
::1
localhost
localhost.localdomain
localhost6
localhost6.localdomain6

  1. for循环基本使用示例,自定义Shell分隔符。默认情况以空格为分隔符。通过IFS来自定义分隔符

以冒号做分隔符 IFS=:

以冒号,分号和双引号作为字段分隔符 IFS=:;"

以换行符作为字段分隔符 IFS=$'\n'

以回车为换行符

[root@rlb /scripts]# cat for-6.sh

!/bin/bash

IFS=$'\n'
for var in cat /etc/hosts
do
echo $var
done
[root@rlb /scripts]# sh for-6.sh
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

以:为分隔符

[root@rlb /scripts]# cat for-7.sh

!/bin/bash

IFS=:
list=head -1 /etc/passwd
for var in $list
do
echo $var
done
[root@rlb /scripts]# sh for-7.sh
root
x
0
0
root
/root
/bin/bash

  1. for循环基本使用示例,C语言风格的for

语法格式

for ((i=0;i<10;i++))
do
commands
done

例1,单个变量,输出1到10之间的数字

[root@rlb /scripts]# cat for-8.sh

!/bin/bash

for ((i=0;i<10;i++))
do
echo num is $i
done

[root@rlb /scripts]# sh for-8.sh
num is 0
num is 1
num is 2
num is 3
num is 4
num is 5
num is 6
num is 7
num is 8
num is 9

例2,多个变量,同时输出1-9的升序和降序

解法一:

[root@rlb /scripts]# cat for-9.sh

!/bin/bash

for ((a=1,b=9;a<10;a++,b--))
do
echo num is $a $b
done

[root@rlb /scripts]# sh for-9.sh
num is 1 9
num is 2 8
num is 3 7
num is 4 6
num is 5 5
num is 6 4
num is 7 3
num is 8 2
num is 9 1

解法二:

[root@rlb /scripts]# cat for-10.sh

!/bin/bash

a=0
b=10
for i in {1..9}
do
let a++;
let b--;
echo num is $a $b
done
[root@rlb /scripts]# sh for-10.sh
num is 1 9
num is 2 8
num is 3 7
num is 4 6
num is 5 5
num is 6 4
num is 7 3
num is 8 2
num is 9 1

  1. 循环语句for场景示例
  2. for循环场景示例一:通过读入文件中的用户,进行批量添加用户。

[root@rlb /scripts]# cat for-11.sh

!/usr/bin/bash

for i in $(cat user.txt)
do
useradd $i &>/dev/null
if [ $? -eq 0 ];then
echo $i 用户创建成功
else
echo $i 用户已存在
fi
done
[root@rlb /scripts]# sh for-11.sh
tt1 用户创建成功
tt2 用户创建成功
tt3 用户创建成功
tt4 用户创建成功
tt5 用户创建成功
tt6 用户创建成功
tt7 用户创建成功
tt8 用户创建成功
tt9 用户创建成功
tt10 用户创建成功

  1. for循环场景示例二:通过读入文件中的用户:密码,进行批量添加用户。

[root@rlb/scripts]# cat user.txt
user01:suibian
user02:suibian2
user03:suibian3
[root@rlb /scripts]# cat for-12.sh

!/usr/bin/bash

for i in \((cat user.txt) do #1.取出来的行,使用awk进行分隔,然后分别赋值给user和pass两个变量 user=\)(echo $i|awk -F ":" '{print \(1}') pass=\)(echo $i|awk -F ":" '{print $2}')

#2.判断用户是否存在
id $user &>/dev/null

#3.用户存在则提示已存在,否则添加用户,然后使用pass变量设定对应的密码
if [ $? -eq 0 ];then
    echo "$user 已存在"
else
    useradd $user 
    echo "$pass" | passwd --stdin $user &>/dev/null
    echo "用户$user 创建成功!"
fi

done
[root@rlb /scripts]# sh for-12.sh
用户user1 创建成功!
用户user2 创建成功!
用户user3 创建成功!

  1. for循环场景示例三:批量创建用户脚本,需要用户输入创建的用户数量,以及需要用户输入创建的前缀。

[root@rlb /scripts]# cat for-13.sh

!/usr/bin/bash

read -p "请输入你要创建的用户前缀: " user_qz
read -p "请输入你要创建的用户数量: " user_num
echo "你创建的用户是 \({user_qz}1 ..\){user_qz}${user_num}"
read -p "你确定要创建以上用户吗?[ y/n ] " readly
case $readly in
y)
for i in $(seq \(user_num) do user=\){user_qz}${i}
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "useradd: user $user already exists"
else
useradd $user
echo "useradd: user $user add successfully."
fi
done
;;
n)
echo "你想好了再创建......"
exit
;;
*)
echo "请不要乱输入...."
exit 1
esac
[root@rlb /scripts]# sh for-13.sh
请输入你要创建的用户前缀: qiu
请输入你要创建的用户数量: 5
你创建的用户是 qiu1 ..qiu5
你确定要创建以上用户吗?[ y/n ] n
你想好了再创建......
[root@rlb /scripts]# sh for-13.sh
请输入你要创建的用户前缀: qiu
请输入你要创建的用户数量: 5
你创建的用户是 qiu1 ..qiu5
你确定要创建以上用户吗?[ y/n ] q
请不要乱输入....
[root@rlb /scripts]# sh for-13.sh
请输入你要创建的用户前缀: qiu
请输入你要创建的用户数量: 5
你创建的用户是 qiu1 ..qiu5
你确定要创建以上用户吗?[ y/n ] y
useradd: user qiu1 add successfully.
useradd: user qiu2 add successfully.
useradd: user qiu3 add successfully.
useradd: user qiu4 add successfully.
useradd: user qiu5 add successfully.

  1. for循环场景示例四:批量创建用户脚本,需要用户输入创建的用户数量(必须是整数),同时还需要用户输入前缀(前缀不能为空)。例如:前缀qls,个数10,代表创建qls1~qls10,总共10个用户。注意:此脚本仅root可执行,其他人无权限执行。

[root@rlb/scripts]# cat for-14.sh

!/usr/bin/bash

if [ ! $UID -eq 0 ] && [ ! $USER == "root" ];then
echo "无权限执行......"
exit
fi

read -p "请输入你要创建的用户前缀: " user_qz
if [ -z $user_qz ];then
echo "请输入有效的值....."
exit
fi

read -p "请输入你要创建的用户数量: " user_num
if [[ ! \(user_num =~ ^[0-9]+\) ]];then
echo "请输入整数"
exit
fi

echo "你创建的用户是 \({user_qz}1 ..\){user_qz}${user_num}"
read -p "你确定要创建以上用户吗?[ y/n ] " readly

case $readly in
y|yes|YES)
for i in $(seq \(user_num) do user=\){user_qz}${i}
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "useradd: user $user already exists"
else
useradd $user
echo "useradd: user $user add successfully."
fi
done

    ;;
n|no|NO)
    echo "你想好了再创建......"
    exit
    ;;
*)
    echo "请不要乱输!"
    exit

esac

  1. for循环场景示例五:批量创建用户脚本,需要用户输入创建的用户数量(必须是整数),同时还需要用户输入前缀(前缀不能为空)。例如:前缀qls,个数10,代表创建qls1~qls10,总共10个用户。注意:此脚本仅root可执行,其他人无权限执行。用户的密码使用随机密码,并保存到某一个指定的文件中。

[root@rlb/scripts]# cat for-15.sh

!/usr/bin/bash

if [ ! $UID -eq 0 ] && [ ! $USER == "root" ];then
echo "无权限执行......"
exit
fi
read -p "请输入你要创建的用户前缀: " user_qz
if [ -z $user_qz ];then
echo "请输入有效的值....."
exit
fi
read -p "请输入你要创建的用户数量: " user_num
if [[ ! \(user_num =~ ^[0-9]+\) ]];then
echo "请输入整数"
exit
fi

echo "你创建的用户是 \({user_qz}1 ..\){user_qz}${user_num}"
read -p "你确定要创建以上用户吗?[ y/n ] " readly

case $readly in
y|yes|YES)
for i in $(seq \(user_num) do user=\){user_qz}${i}
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "useradd: user \(user already exists" else user_pass=\)(echo $((RANDOM))|md5sum |cut -c 2-10)
useradd \(user echo "\)user_pass" | passwd --stdin $user &>/dev/null
echo "用户: $user 密码: $user_pass" >> /tmp/user.log
echo "useradd: user $user add successfully. cat /tmp/user.log"
fi
done

    ;;
n|no|NO)
    echo "你想好了再创建......"
    exit
    ;;
*)
    echo "请不要乱输!"
    exit

esac

  1. for循环场景示例六:批量删除(用户需要输入用户前缀及数字),有就删除,没有就提示没有该用户。

[root@rlb /scripts]# cat for-16.sh

!/usr/bin/bash

if [ ! $UID -eq 0 ] && [ ! $USER == "root" ];then
echo "无权限执行......"
exit
fi

read -p "请输入你要删除的用户前缀: " del_qz
if [ -z $del_qz ];then
echo "请输入有效的值....."
exit
fi

read -p "请输入你要删除的用户数量: " user_num
if [[ ! \(user_num =~ ^[0-9]+\) ]];then
echo "请输入整数"
exit
fi

echo "你删除的用户是 \({del_qz}1 ..\){del_qz}${user_num}"
read -p "你确定要删除以上用户吗?[ y/n ] " readly

case $readly in
y|yes|YES)
for i in $(seq \(user_num) do user=\){del_qz}${i}
id $user &>/dev/null
if [ $? -eq 0 ];then
userdel -r \(user echo "\)user 用户删除成功....."
else
echo "$user 用户不存在..."
fi
done
;;
n|no|NO)
echo "你想好了再删除......"
exit
;;
*)
echo "请不要乱输!"
exit
esac

[root@rlb /scripts]# sh for-16.sh
请输入你要删除的用户前缀: qiu
请输入你要删除的用户数量: 5
你删除的用户是 qiu1 ..qiu5
你确定要删除以上用户吗?[ y/n ] y
qiu1 用户删除成功.....
qiu2 用户删除成功.....
qiu3 用户删除成功.....
qiu4 用户删除成功.....
qiu5 用户删除成功.....

  1. for循环场景示例七:批量探测主机存活状态及22端口状态

1)需要用循环,循环254次
2)探测主机使用ping命令

[root@rlb /scripts]# cat for-17.sh

!/usr/bin/bash

/tmp/ip.log
for i in {1..254}
do
{
ip=172.16.1.$i
ping -c 1 -W 1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "\(ip is ok" echo "\)ip is ok" >>/tmp/ip.log
else
echo "$ip is down"
fi
} &
done
wait
echo "Sao Miao IP is Done"

echo "Test SSH Port Starting......"

IFS=$'\n'
for i in \((cat /tmp/ip.log) do port_ip=\)(echo $i |awk '{print $1}')
nmap $port_ip |grep "22" &>/dev/null
if [ $? -eq 0 ];then
echo "\(port_ip 22 is ok!!" echo "\)port_ip 22 is ok!!" >> /tmp/port.log
fi
done

  1. for循环场景示例八:编写一个上课随机点名脚本。

1.需要有名字
2.需要循环的打印这些名单
3.随机挑选一个数字进行打印

[root@rlb /scripts]# cat for-18.sh

!/usr/bin/bash

1.统计文件的行数

number=$(wc -l /root/student.txt|awk '{print \(1}') #2.整个循环20次 for i in {1..20} do #3.通过random取随机数,但不超过文件的行数 stu_num=\)((RANDOM % \(number + 1 )) #4.循环一次打印一次随机的名 sed -n "\){stu_num}p" /root/student.txt
#5.等待0.1s之后再循环下一次
sleep 0.10
done
#6.将最后一次循环提取的人名赋值给name_stu
name_stu=\((sed -n "\){stu_num}p" /root/student.txt)
#7.自定义输出人名,外加颜色,好看。
echo -e "天选子: \033[32m ${name_stu}....\033[0m"

  1. for循环场景示例九:使用for循环实现数据库的分库分表备份。

1.怎么备份数据库
mysqldump -uroot -p123.com --single-transaction -B world > world_database.sql
2.怎么备份数据库的表
mysqldump -uroot -p123.com --single-transaction world city > world_city_tables.sql
3.备份到哪儿
/mysql_dump/oldboy/city_2019_07_16.sql

!/usr/bin/bash

db_name=\((mysql -uroot -p123.com -e "show databases;"|sed 1d |grep -v ".*_schema") DB_User=root DB_Pass=123.com Date=\)(date +%F)

for database_name in \(db_name do #1.准备每个库的目录 DB_Path=/mysql_dump/\)database_name
if [ ! -d $DB_Path ];then
mkdir -p \(DB_Path fi #2.备份数据库 mysqldump -u\)DB_User -p$DB_Pass --single-transaction -B $database_name > \(DB_Path/\){database_name}_${Date}.sql
echo -e "\033[31m $database_name 数据库
已经备份完成..... \033[0m"

#3.备份数据库的每一个表
tb_name=$(mysql -u$DB_User -p$DB_Pass -e "use ${database_name};show tables;"|sed 1d)

#4.批量的备份数据库的表
for table_name in $tb_name
do
    #5.备份数据库的表数据
    mysqldump -u$DB_User -p$DB_Pass --single-transaction \
    $database_name $table_name > $DB_Path/${database_name}_${table_name}_tables_${Date}.sql
    echo -e "\033[32m 备份$database_name 的数据库中的 $table_name 数据表,备份成功 \033[0m"
done

done

  1. for循环场景示例十:用于判断mysql数据库主从的脚本,需要邮件通知。

1.判断io线程和sql线程是否都为yes,如果是则成功。
2.如果io线程失败,则直接邮件通知,如果sql线程失败,则检查是什么错误状态码,根据状态码修复。
3.无法修复,或任何错误代码太复杂建议,直接发邮件通知管理员。

[root@rlb/scripts]# cat for-20.sh

!/usr/bin/bash

IO_Status=$(mysql -uroot -p123.com -e "show slave status\G"|awk '/Slave_IO_Running/ {print \(2}') SQL_Status=\)(mysql -uroot -p123.com -e "show slave status\G"|awk '/Slave_SQL_Running/ {print $2}')

slave_sql_error_message(){
mysql -uroot -p123.com -e "show slave status\G"|grep "Last_SQL" > /tmp/sql_err.log
mail -s "MySQL Master SLave SQL Error $(date +%F)" [email protected] < /tmp/sql_err.log
echo "邮件通知成功......"
}

slave_io_error_message(){
mysql -uroot -p123.com -e "show slave status\G"|grep "Last_IO" > /tmp/io_err.log
mail -s "MySQL Master SLave IO Error $(date +%F)" [email protected] < /tmp/io_err.log
echo "邮件通知成功......"
}

if [ $IO_Status == "Yes" ] && [ $SQL_Status == "Yes" ];then
echo "MySQL主从正常"
else
#1.判断IO异常
if [ ! $IO_Status == "Yes" ];then
slave_io_error_message
exit
fi

#2.判断SQL异常
if [ ! $SQL_Status == "Yes" ];then
    SQL_Err=$(mysql -uroot -p123.com -e "show slave status\G"|awk '/Last_SQL_Errno/ {print $2}')
    #3.精细化判断主从不同步的问题
    case $SQL_Err in 
        1007)
            echo "主从的SQL出现问题,尝试使用set global sql_slave_skip_counter=1; 跳过错误"
            sleep 2
            mysql -uroot -p123.com -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
            SQL_Err_1=$(mysql -uroot -p123.com -e "show slave status\G"|awk '/Last_SQL_Errno/ {print $2}')
            if [ $SQL_Err_1 -eq 0 ];then
                echo "尝试跳过了一次,恢复MySQL数据库成功"
            else
                slave_sql_error_message
            fi
                ;;
        1032)
                slave_sql_error_message
                ;;
        *)
                slave_sql_error_message
    esac
fi

fi

  1. 循环语句while基本概述
    while循环语句,只要条件成立就反复执行对应的命令操作,直到命令不成立或为假

  2. while循环基础语法

当条件测试成立(条件测试为真),执行循环体

while 条件测试

do
循环体

done

  1. while循环基本使用示例,降序输出10到1的数字

[root@rlb /scripts]# cat while-1.sh

!/bin/bash

var=10
while [ $var -gt 0 ]
do
echo \(var var=\)[$var-1]
done

简单加法表

[root@rlb/scripts]# cat while-2.sh

!/usr/bin/bash

a=1
b=10
while [ $a -le 10 ]
do
sum=$(( $a + $b ))
echo $a + $b = $sum
let a++
let b--
done

  1. while循环基本使用示例,输出如下图,两数相乘。

自增

[root@rlb/scripts]# cat while-3.sh

!/bin/bash

num=9
while [ $num -ge 1 ]
do
sum=$(( $num * \(num )) echo "\)num * $num = \(sum" num=\)[$num-1]
done

自减

[root@rlb /scripts]# cat while-4.sh

!/bin/bash

num=1
while [ $num -le 9 ]
do
sum=$(( $num * \(num )) echo "\)num * $num = \(sum" num=\)[$num+1]
done

  1. 循环语句while场景示例
  2. 使用while读入文件方式,批量创建用户,while默认按行取值

[root@rlb /scripts]# cat while-5.sh

!/usr/bin/bash

while read line
do
#1.判断用户是否存在
id $line &>/dev/null
if [ $? -eq 0 ];then
echo "User: $line already exists"
else
useradd $line &>/dev/null
echo "User: $line Create Ok"
fi
done<user.txt
[root@rlb/scripts]# cat user.txt
ttt1
ttt2
ttt3
ttt4
ttt5

  1. 使用while读入文件方式,批量创建用户以及密码[user:passwd]

[root@rlb /scripts]# cat while-6.sh

!/usr/bin/bash

while read line
do
user=$(echo $line |awk -F ':' '{print \(1}') pass=\)(echo $line |awk -F ':' '{print $2}')
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "用户已存在"
else
useradd $user && echo $pass |passwd --stdin $user &>/dev/null
echo "用户: $user is ok 密码: $pass "
fi
done<user.txt
[root@rlb /scripts]# cat user.txt
tttt1:ggdfg
tttt2:fbthb
tttt3:fbtbht
tttt4:bthht
tttt5:frgt

  1. 使用while读入文件方式,批量创建用户,并赋予一个随机密码

[root@rlb /scripts]# cat while-7.sh

!/usr/bin/bash

while read line
do
pass=$(echo $RANDOM |md5sum |cut -c1-10)
id $line &>/dev/null
if [ $? -eq 0 ];then
echo "用户已存在"
else
useradd $line && echo $pass |passwd --stdin $line &>/dev/null
echo "用户: $line is ok 密码: $pass "
fi
done<user.txt

  1. while循环场景示例:完成如下猜数字游戏

1) 随机输出一个1-100的数字 echo $((RANDOM % 100 + 1 ))
2) 要求用户输入的必须是数字(数字处加判断)

3) 如果比随机数小则提示比随机数小了
大则提示比随机数大了

4) 正确则退出
错误则继续死循环

5) 最后统计总共猜了多少次(成功多少次,失败多少次)

!/usr/bin/bash

sj=$(echo $((RANDOM % 100 + 1 )))
i=0
while true
do
read -p "请输入一个你需要猜的数字: " cc
if [[ ! \(cc =~ ^[0-9]+\) ]];then
echo "请输入整数"
continue
fi

#将用户的输入的数字与随机数进行比较

if [ $cc -gt $sj ];then 
    echo "你猜的太大" 
elif [ $cc -lt $sj ];then 
    echo "你猜的太小,继续加油" 
else 
    echo "猜对了....." 
    break 
fi 
#进行数值自增

let i++ 

done
echo "你总共失败了多少次 $i"
echo "你总共猜了多少次 $(( $i + 1 ))"

  1. while循环场景示例:随机点菜脚本

菜单显示以下内容

!/bin/bash

menu(){
echo -e "*********************************"
echo -e "* 1.青椒土豆丝\t\t"
echo -e "
2.麻婆豆腐\t\t"
echo -e "
3.红烧大盘鸡\t元\t"
echo -e "
4.宫保鸡丁\t\t"
echo -e "
5.鱼香肉丝\t\t"
echo -e "
6.西红柿鸡蛋\t\t"
echo -e "
7.红烧肉\t\t"
echo -e "
8.酸菜鱼\t元\t"
echo -e "
9.结束点菜\t\t"
echo -e "
********************************"
}
num=echo $((RANDOM%9+1))
while true
do
menu
read -p "请开始点菜:" num
case $num in
1)
echo "您点了青椒土豆丝"
;;
2)
echo "您点了麻婆豆腐"
;;
3)
echo "您点了红烧大盘鸡"
;;
4)
echo "您点了宫保鸡丁"
;;
5)
echo "您点了鱼香肉丝"
;;
6)
echo "您点了西红柿鸡蛋"
;;
7)
echo "您点了红烧肉"
;;
8)
echo "您点了酸菜鱼"
;;
9)
echo "稍等,马上上菜!"
exit
esac
done

  1. 内置跳出循环语句指令
    在我们使用循环语句进行循环的过程中,有时候需要在未达到循环结束条件时强制跳出循环,那么Shell给我们提供了内置方法来实现该功能:exit、break、continue

  2. exit,退出整个程序

[root@rlb /scripts]# cat for-exit.sh

!/bin/bash

for i in {1..3}
do
echo "123"
exit
echo "456"
done
echo "done ......"

执行结果

[root@rlb /scripts]# sh for-exit.sh
123

  1. break,结束当前循环,或跳出本层循环

[root@rlb /scripts]# cat for-break.sh

!/bin/bash

for i in {1..3}
do
echo "123"
break
echo "456"
done
echo "done ......"

执行结果

[root@rlb /scripts]# sh for-break.sh
123
done ......

  1. continue,忽略本次循环剩余的代码,直接进行下一次循环。

[root@rlb /scripts]# cat for-continue.sh

!/bin/bash

for i in {1..3}
do
echo "123"
continue
echo "456"
done
echo "done ......"

执行结果

[root@rlb /scripts]# sh for-continue.sh
123
123
123
done ......

  1. 习题:先扫描内网网段所有主机,存活的则下发当前主机的公钥。

[root@rlb /scripts]# cat key.sh

!/bin/bash

删除旧的密钥对

rm -f ~/.ssh/id_rsa*

创建密钥对

ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "" &>/dev/null

批量分发公钥的操作

for ip in {2..10}
do
ping -W1 -c1 172.16.1.$ip &>/dev/null
if [ $? -eq 0 ];then
sshpass -p123 ssh-copy-id -p2222 -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip &>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[32mhost 172.16.1.\(ip Distribution of the secret key Success!!! \033[0m" echo else echo -e "\033[31mhost 172.16.1.\)ip Distribution of the secret key Failed !!! \033[0m"
echo
fi
else
echo -e "\033[31mhost 172.16.1.$ip Destination Host Unreachable! \033[0m"
continue
fi
done

猜你喜欢

转载自www.cnblogs.com/ronglianbing/p/11711904.html
今日推荐