title: week_2
tags:
- null
categories: - null
date: 2022-08-07 14:35:14
week2
- 运行脚本可以显示出本机的ip地址
- 如果ip地址中有3这个数字,那么就打印出当前的系统时间
- 如果ip地址中不含3这个数字,就批量建立用户magedu_00, magedu_01, ... magedu_100并且所有用户同属于magedu组
- 打印出/etc/passwd这个文件中可以登陆的用户(非/usr/sbin/nologin)
- yum安装nginx服务,并且启动该服务
- 一个脚本完成
#!/bin/bash
########################################
#Author:Catyer
#time:2022-08-02_20:56:32
#filename:1.sh
#Script description:
########################################
##适应不同的机器网卡名
DATE=`date`
IP=`hostname -I | awk '{print $1}'`
OS=`awk -F= '/ID/{print $2}' /etc/os-release | head -n 1 | sed 's/\"//g'`
#pass=`cat /dev/urandom | tr -dc [:alnum:] | head -c 8`
IP(){
# local IP=`ifconfig eth0 | awk /netmask/'{print $2}'`
echo "本机IP:$IP"
}
IPCHECK(){
#DATE=`date`
echo $IP
#IPcheck=`ifconfig eth0 | awk /netmask/'{print $2}' | grep 3`
hostname -I | awk '{print $1}' | grep 3 &> /dev/null
if [ $? -eq 0 ] ;then
echo "$DATE"
else
[ "grep magedu /etc/group" ] && echo "group exist" || groupadd madedu
for i in {1..20};do
local pass=`cat /dev/urandom | tr -dc [:alnum:] | head -c 8`
if [ $i -lt 10 ];then
useradd magedu_0$i -g magedu
echo $pass | passwd --stdin magedu_0$i &> /dev/null ;echo "magedu_0$i create"
echo "username:magedu_0$i passwd:$pass" >> /script/homework/pass.txt
else
useradd magedu_$i -g magedu
echo $pass | passwd --stdin magedu_$i &> /dev/null ;echo "magedu_$i create"
echo "username:magedu_$i passwd:$pass" >> /script/homework/pass.txt
fi
done
fi
cat /script/homework/pass.txt
}
USER(){
grep -v "/sbin/nologin" /etc/passwd | awk -F: '{print $1}'
}
NGINX(){
if [ $OS == "centos" ] ;then
rpm -q nginx && echo "nginx install" || yum -y install nginx;systemctl enable --now nginx
elif [ $OS == "ubuntu" ] ;then
##ubuntu自动启动服务
apt list --installed | grep nginx && echo "ubuntu nginx is installed" || apt update; apt -y install nginx
else
echo "invalid $OS"
fi
echo "nginx test" > /usr/share/nginx/html/index.html
systemctl enable --now nginx
curl $IP
}
while :
do
cat <<EOF
========================我是分隔符========================
week 2 homework
1.输出本机IP地址
2.判断IP地址是否含有3(打印系统时间/创建用户)
3.打印出/etc/passwd可以登陆的用户(非/usr/sbin/nologin)
4.安装nginx服务,并且启动该服务(centos/ubuntu)
5.exit
========================我是分隔符========================
EOF
read -p "please input your num: " num ##这里注意不要写成$num变量
case $num in
1) IP;;
2) IPCHECK;;
3) USER;;
4) NGINX;;
5|e|E|exit|Exit) break ;;
*) echo "error input"
esac
done
实现效果
1.运行界面
2.输出IP地址
3.判断是否有3,否则创建用户
创建随机密码
4.输出nologin用户
5.安装nginx
nginx测试页:nginx test
标签:IP,magedu,echo,nginx,pass,print,week2 From: https://www.cnblogs.com/catyer/p/16637834.html