显示系统信息
[root@wenzi data]#cat systemInfo.sh
#/bin/bash
RED="\E[1;31m"
GREEN="\E[1;32m"
END="\E[0m"
echo -e "$GREEN----------------------Host systeminfo--------------------$END"
echo -e "HOSTNAME: $RED`hostname`$END"
echo -e "IPADDR: $RED`ifconfig ens160 | grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' | head -n 1`$END"
echo -e "OSVERSION: $RED`cat /etc/redhat-release`$END"
echo -e "KERNEL: $RED`uname -r`$END"
echo -e "CPU: $RED`lscpu | grep 'Model name'| tr -s ' ' | cut -d : -f 2`$END"
echo -e "MEMORY: $RED`free -h | grep 'Mem' | tr -s ' ' : | cut -d ':' -f 2`$END"
echo -e "DISK: $RED`lsblk | grep '^nv' | tr -s ' ' | cut -d " " -f 4`$END"
echo -e "$GREEN---------------------------------------------------------$END"
说明:
grep -E 开启拓展正则表达式,-o 仅显示匹配到的数据
([0-9]{1,3}\.){3}[0-9]{1,3}中[0-9]{1,3}表示1到3位数字,{3}表示重复3次,所以整体就是匹配IP地址
tr -s 对指定的字符串去重复
cut 使用对象是一行信息,-d 指定分割字符,-f 依据-d的分割字符将一段信息分为数段,用-f取出第几段
^nv 指以nv开头
批量创建用户并设置随机密码
[root@wenzi data]#cat batchCreateUser.sh
#!/bin/bash
userNum=$1
for ((i=1;i<=${userNum};i+=1))
do
useradd wenzi$i
pwd=`cat /dev/urandom | tr -d -c '[:alnum:]' | head -c 12`
echo $pwd | passwd --stdin wenzi$i &> /dev/null
echo wenzi$i:$pwd >> /data/user.log
echo "wenzi$i is created"
done
[root@wenzi data]#bash batchCreateUser.sh 5
wenzi1 is created
wenzi2 is created
wenzi3 is created
wenzi4 is created
wenzi5 is created
[root@wenzi data]#cat /data/user.log
wenzi1:aBfx6xKXXWXd
wenzi2:zeiXcSdeECga
wenzi3:XXWhxovUy9o1
wenzi4:mk6wF4gqQxbP
wenzi5:YvcHWo6UYQcP
#删除生成的用户
[root@wenzi data]#for i in {1..5};do userdel -r wenzi$i;done
说明:
/dev/urandom是随机数生成器
[:alnum:] 代表英文大小写字符及数字,即 0-9,A-Z,a-z
tr的-d选项:删除信息中指定内容;-c选项:使用指定字符串的补集替换该字符串;所以tr删除的是非'[:alnum:]'的内容
执行远程主机的脚本
#主机192.168.29.142
[root@wenzi ~]#yum -y install httpd
[root@wenzi ~]#vim /var/www/html/hello.sh
#!/bin/bash
echo "hello world"
[root@wenzi ~]#systemctl start httpd
#主机192.168.29.141
[root@wenzi data]#curl http://192.168.29.142/hello.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 31 100 31 0 0 31000 0 --:--:-- --:--:-- --:--:-- 31000
hello world
[root@wenzi data]#curl -s http://192.168.29.142/hello.sh | bash
hello world
rm命令的安全实现
[root@wenzi data]#vim rm.sh
#!/bin/bash
warning_color="echo -e \E[1;31m"
end="\E[0m"
dir=/tmp/`date +%F%T`
mkdir $dir
mv $* $dir
${warning_color}Move $* to $dir $end
[root@wenzi data]#chmod 777 /data/rm.sh
[root@wenzi data]#alias rm='/data/rm.sh'
[root@wenzi data]#touch {1..3}.txt
[root@wenzi data]#ll
total 4
-rw-r--r-- 1 root root 0 Jul 22 10:43 1.txt
-rw-r--r-- 1 root root 0 Jul 22 10:43 2.txt
-rw-r--r-- 1 root root 0 Jul 22 10:43 3.txt
-rw-r--r-- 1 root root 138 Jul 22 10:42 rm.sh
[root@wenzi data]#rm *.txt
Move 1.txt 2.txt 3.txt to /tmp/2023-07-2210:45:20
说明:
$*:位置变量,代表除了$0(脚本本身名字)以外,所有的参数,如上即 “opt1 opt2 opt3 opt4“,每个变量之间用空格分割,共用一个双引号
检查软件包是否安装
[root@wenzi wenzi]# cat test.sh
#!/bin/bash
pkg=$1
rpm -q $1 > /dev/null
if [ $? -eq 0 ];then
echo "$1 is already installed."
else
echo "$1 is not installed!"
fi
[root@wenzi wenzi]# ./test.sh httpd
httpd is already installed.
[root@wenzi wenzi]# ./test.sh nginx
nginx is not installed!
检查服务状态
[root@wenzi wenzi]# cat test.sh
#!/bin/bash
if pgrep $1 > /dev/null;then
echo "$1 is running"
else
echo "$1 is not running"
fi
[root@wenzi wenzi]# ./test.sh httpd
httpd is running
[root@wenzi wenzi]# ./test.sh nginx
nginx is not running