一、条件语句 if
1.1、单分支
if [ 1 -eq 1 ];then
echo "haha"
fi
1.2、双分支
if [ 1 -eq 2 ];then
echo "haha"
else
echo "shadan"
fi
1.3、多分支
if [ 1 -eq 2 ];then
echo "haha"
elif [ 1 -eq 1 ];then
echo "123"
else
echo "shadan"
fi
脚本1,yum源,防火墙配置
#!/bin/bash
str=`cat /etc/redhat-release |awk '{print $4}'|awk -F '.' '{print $1}'`
version=0
mkdir /etc/yum.repos.d/bak &>/dev/null
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak/ &>/dev/null
if [ $str == 7 ];then
version=7
else
version=6
fi
echo $version
if [ $version -eq 7 ];then
#关闭防火墙及开机自启
systemctl stop firewalld
systemctl disable firewalld
#关闭selinux
setenforce 0
sed -i 7s/enforcing/disabled/g /etc/selinux/config
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#配置yum源
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
elif [ $version -eq 6 ];then
#关闭防火墙及开机自启
iptables -F
service iptables stop
chkconfig iptables off
#关闭selinux
setenforce 0
sed -i 7s/enforcing/disabled/g /etc/selinux/config
#配置yum源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
else
echo "please check your /etc/redhat-release file "
fi
yum clean all
yum makecache
二、条件语句 case
2.1、格式
case 变量 in
匹配条件1)
命令序列 1
;;
匹配条件2)
命令序列 2
;;
匹配条件3)
命令序列 3
;;
*)
⽆匹配后命令列
esac
脚本1,批量删除用户
#!/bin/bash
while true
do
read -p "请输入你要删除的账户前缀>> " delname
read -p "请输入你要删除的账户数量>> " delnum
echo "你将删除如下账户:
用户前缀是: $delname
用户数量是: $delnum"
read -p "你确定要删除(y|yes/n|no)?" ch
case $ch in
y|yes)
for i in `seq $delnum`
do
username=$delname$i
id $username &>/dev/null
if [ $? -eq 0 ];then
userdel -r $username
echo "删除成功 $username"
else
echo "没有该用户 $username"
fi
done
break
;;
n|no)
echo "取消删除"
exit 1
;;
*)
echo "错误输入,请重新输入"
esac
done
脚本2,系统命令工具箱
#!/bin/bash
menu(){
cat << EOF
=====================system info====================
h、显示帮助命令
f、显示磁盘分区
d、显示磁盘挂载
m、查看内存使用
u、查看系统负载
q、退出脚本
=====================system info====================
EOF
}
menu
while true
do
read -p "请输入对应序号>> " ch
case $ch in
h)
clear
menu
;;
f|d)
lsblk
;;
m)
free -h
;;
u)
uptime
;;
q)
exit
;;
*)
echo "error input"
esac
done
三、交互脚本expect
执行某些命令时,需要回答,例如 ssh登录 需要输入yes和密码
[root@localhost ~]# ssh root@100.100.100.101
The authenticity of host '100.100.100.101 (100.100.100.101)' can't be established.
ECDSA key fingerprint is SHA256:O84EC9RloVc32hoAxnpkn29AX9kM4z+xMgZ3h1CJkj8.
ECDSA key fingerprint is MD5:a8:2c:88:fd:64:2f:ad:8d:69:75:37:b9:19:2c:8c:57.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '100.100.100.101' (ECDSA) to the list of known hosts.
root@100.100.100.101's password:
Last failed login: Tue Jan 28 02:10:31 CST 2020 on tty1
There was 1 failed login attempt since the last successful login.
Last login: Mon Jan 27 22:23:10 2020
3.2 我们使用 expect 完成上述操作
!/bin/expect
spawn ssh root@100.100.100.101
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "123\r"}
}
interact 显示执行过程
expect "#" 当出现 # 号符执⾏行行如下命令
send "echo haha\r"
send "exit\r"
expect eof
注意,不能使用 bash执行,而是赋予权限,使用 source 脚本名 或者 ./脚本名 执行
脚本,使用 expect 进行密钥对分发
#!/bin/bash
#获取当前网段可用ip
read -p "请输入网段..." ip_pre
rm -rf ip.txt &>/dev/null
for i in {1..254}
do
{
ip=$ip_pre.$i
ping -c1 -w1 $ip >/dev/null
if [ $? -eq 0 ];then
echo "$ip"|tee -a ip.txt
fi
}&
done
wait
#生成密钥对
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -P "" -f ~/.ssh/id_rsa
fi
#使用expect分发密钥
while read line
do
/usr/bin/expect <<-EOF
spawn ssh-copy-id $line -f
expect {
"yes/no" {send "yes\r";exp_continue}
"password" {send "123\r"}
}
expect eof
EOF
done<ip.txt