一、随机数
echo $RANDOM 默认产生0~32767随机整数
echo $[$RANDOM%2] 产生0~1之间的随机数
echo $[$RANDOM%3] 产生0~2之间的随机数
echo $[$RANDOM%4] 产生0~3之间的随机数
echo $[$RANDOM%10] 产生0~9之间的随机数
echo $[$RANDOM%90+10] 产生10~99之间的随机数
echo $[$RANDOM%101] 产生0~100之间的随机数
echo $[$RANDOM%51+50] 产生50~100之间的随机数
echo $[$RANDOM%900+100] 产生100~999之间的随机数
1、随机产生139开头的电话号码1000个
#!/bin/env bash
file=/home/phonenum.txt
for ((i=1;i<=1000;i++))
do
n1=$[$RANDOM%10]
n2=$[$RANDOM%10]
n3=$[$RANDOM%10]
n4=$[$RANDOM%10]
n5=$[$RANDOM%10]
n6=$[$RANDOM%10]
n7=$[$RANDOM%10]
n8=$[$RANDOM%10]
echo "139$n1$n2$n3$n4$n5$n6$n7$n8" >> $file
done
2、随机抽出5位幸运观众
#!/bin/env bash
phone=/home/phonenum.txt
for ((i=1;i<=5;i++))
do
line=`wc -l $phone | cut -d' ' -f1`
luck_line=$[RANDOM%$line+1]
luck_num=`head -$luck_line $pone|tail -1`
echo "139****${luck_num:7:4}"
echo $luck_num >> luck.txt
sed -i "/$luck_num/d" $phone
done
3、批量创建用户(密码随机产生)
#!/bin/bash
echo user{1..5}:test$[$RANDOM%9000+1000]#@~ | tr ' ' '\n' >> user_pass.file
for ((i=1;i<=5;i++))
do
user=`head -$i user_pass.file|tail -1|cut -d: -f1`
pass=`head -$i user_pass.file|tail -1|cut -d: -f2`
useradd $user
echo $pass|passwd --stdin $user
done
二、嵌套循环
打印图案
1
12
123
1234
12345
#!/bin/bash
for ((y=1;y<=5;y++))
do
for ((x=1;x<=$y;x++))
do
echo -n $x
done
echo
done
5
54
543
5432
54321
#!/bin/bash
y=1
while (($y<=5))
do
for ((x=5;x>=6-$y;x--))
do
echo -n $x
done
echo
let y++
done
三、补充扩展
1、shift位移
#!/bin/bash
sum=0
while [ $# -ne 0 ]
do
let sum=$sum+$1
shift #让脚本后面的参数向左移一位
done
echo sum=$sum
如果是shift 2,脚本后面加1 2 3 4 5 6 7 8 9,结果是1+3+5+7+9=25
2、expect自动应答
(一)远程登录到server上
#!/usr/bin/expect
spawn ssh [email protected]
expect {
"(yes/no)?" { send "yes\r";exp_continue }
"password:" { send redhat\r }
}
interact
(二)定义变量
#!/usr/bin/expect
set ip 192.168.1.1
set pass redhat
set timeout 5
spawn ssh root@$ip
expect {
"(yes/no)?" { send "yes\r";exp_continue }
"password:" { send "$pass\r" }
}
interact
(三)远程登录到server上操作
#!/usr/bin/env expect
set ip 192.168.1.1
set pass redhat
set timeout 5
spawn ssh root@$ip
expect {
"(yes/no)?" { send "yes\r";exp_continue}
"password:" { send "pass\r" }
}
expect "#"
send "rm -rf /tmp/*\r"
send "hostname\r"
send "touch /tmp/file{1..3}\r"
expect eof
(四)使用位置参数
#!/usr/bin/env expect
set ip [ lindex $argv 0 ]
set pass [ lindex $argv 1 ]
set timeout 5
spawn ssh root@$ip
expect {
"(yes/no)?" { send "yes\r";exp_continue }
"password:" { send "$pass\r" }
}
interact
(五)多台服务器上创建用户
#!/bin/bash
while read ip pass
do
/usr/bin/expect <<-END &>/dev/null
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$pass\r" }
}
expect "#" { send "useradd user1;rm -rf /tmp/*;exit\r" }
expect eof
END
echo "$ip服务器用户创建完成"
done < ip.txt
ip.txt格式为
192.168.1.1 redhat
192.168.1.2 redhat
四、脚本练习
将跳板机yunwei用户的公钥推送到其他网络可达的服务器上
功能一、管理员用户判断yunwei用户是否存在,安装expect软件
#!/bin/env bash
{
id yunwei
[ $? -ne 0 ] && useradd yunwei && echo 123|passwd --stdin yunwei
} &>/dev/null
rpm -q expect
[ $? -ne 0 ] && yum install expect -y && echo "expect安装完成"
功能二、判断是否能ping通主机,yunwei用户推送公钥
#!/bin/env bash
home_dir=/home/yunwei
[ -f $home_dir/.ssh/id_rsa.pub ] && ssh-keygen -P '' -f $home_dir/.ssh/id_rsa &>/dev/null
ip_txt=$home_dir/ip.txt
for i in `cat $ip_txt`
do
ip=`echo $i|cut -d: -f1`
pass=`echo $i|cut -d: -f2`
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo $ip >> ~/ip_up.txt
/usr/bin/expect <<-END &>/dev/null
spawn ssh-copy-id root@$ip
expect "(yes/no)" send { "yes\n";exp_continue }
expect "password:" send { "$pass\r" }
expect eof
END
else
echo $ip >> $home_dir/ip_down.txt
fi
done
#测试验证
remote_ip=`head -1 ~/ip_up.txt`
ssh root@$remote_ip hostname
[ $? -eq 0 ] && echo "公钥推送成功"
yunwei用户sudo授权:
visudo
root ALL=(ALL) ALL
yunwei ALL=(root) NOPASSWD:ALL,!/sbin/shutdown,!/sbin/init,!/bin/rm -rf /