1.在/usr/目录下,创建test00目录,进入该目录,连续创建20个目录,目录的名称是stu01~20。
逻辑过程要求:该目录下如果已经存在需要创建的目录,则自动跳过创建命令动作。
touch sb.sh
sudo gedit sb.sh
#!/bin/bash
if [ ! -d $stu.$i ];then
for((i=01;i<=20;i++))
do
sudo mkdir stu.$i;
ls stu.$i;
done
else
echo" exist"
fi
bash sb.sh
2.创建用户stu01,
sudo useradd -d stu01
要求:a.用户拥有自己的目录,
b.允许远程登录,
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
c.配置账户密码最短使用期限为 10天,最长使用期限为 90 天。
sudo chage stu01 -M 90 -m 10 -W 5
overdue=$(chage -l ${username} | grep '密码过期时间' | awk '{print $3,$4,$5}')
echo 账号${username}密码过期时间为"${overdue}"
配置完成后,过期的提醒消息到屏幕上方便阅读,该信息自行定义。
//第二种完整shell
#!/bin/bash
username=stu01
#创建用户stu01,其密码为123456,并且拥有自己的文件夹,文件夹名为/home/stu01
useradd -d /home/${username} -m -s /bin/bash -p $(echo 123456 | openssl passwd -1 -stdin) ${username}
#允许stu01用户远程登录
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
#配置用户最短使用期限10天,最长使用期限为90天
sudo chage -m 10 -M 90 ${username}
#提取出账号过期时间
overdue=$(chage -l ${username} | grep '密码过期时间' | awk '{print $3,$4,$5}')
echo 账号${username}密码过期时间为"${overdue}"
3.ping网段,
要求:
1.获取当前服务器的ip,比如172.17.31.130
//1 端中使用命令“ifconfig-a”,如果提示找不到“ifconfig”,根据提示安装"net-tools",运行命令"sudo apt install net-tools"再运行“ifconfig-a”k可以查看网络连接信息
2 hostname -i
2.则ping 172.17.31.30~130范围内的机器,100台的数量
ping-c 100 ip地址
3.给出ping通了多少台机器的相关信息,显示到屏幕上。
#!/bin/bash
#获取当前服务器的IP地址
//IP=`/sbin/ifconfig -a|grep inet|grep -v inet6|grep -v 127.0.0.1|awk '{print $2}'|tr -d "addr:"`//求IP 可要可不要
#以.分割IP地址
IP_ARR=(${IP//./ })
#获取网段
NETWORK=${IP_ARR[0]}.${IP_ARR[1]}.${IP_ARR[2]}.
num=0
for i in {125..130}
do
#检查是否存在该IP地址
ping -c 1 -w 1 ${NETWORK}$i &>/dev/null
if [ $? -eq 0 ];then
echo 找到了${NETWORK}${i}
num=`expr ${num} + 1`
fi
done
echo Ping通了${num}台服务器
4 文本信息筛出
a.使用history命令,获取结果数据,不带行号。将结果重定向输出到文件command.log中
cat ~/.bash_history>command.log
b.逐一读取该文件中的每一行内容后,然后生成新文件command_01.log
要求:文件command_01.log跟文件command.log类似,行数不变,
凡是非sudo开头的行,在开头添加符号“#”
比如,某行“sh test.sh”则修改为“#sh test.sh”
touch sb.sh
sudo gedit sb.sh
#!/bin/bash
while read line
do
if [[ $line != *"sudo"* ]]
then
#获取这一行的第一个参数
echo \#${line}>>command_01.log
else
echo ${line}>>command_01.log
fi
done
bash sb.sh
5.更换软件源(默认21.10系统版本)
编写程序,选择输入1或者2或者3
则将当前系统的软件源,对应更换为清华大学,浙江大学,中国科技大学的镜像源
更换信息完成后,重建软件源的缓存和更新源。
#!/bin/bash
source_url=""
#获取传入的参数
case $1 in
"1")
#清华源
source_url="https:\/\/mirror.tuna.tsinghua.edu.cn\/"
;;
"2")
#浙大源
source_url="http:\/\/mirrors.zju.edu.cn\/"
;;
"3")
#中科大源
source_url="https:\/\/mirrors.ustc.edu.cn\/"
;;
*)
echo "参数错误"
exit 1
;;
esac
#换源
sudo sed -i -r "s/(http|https):\/\/[a-z.]+\//${source_url}/" /etc/apt/sources.list
#更新缓存
sudo apt-get clean
#更新源
sudo apt-get update
一、服务器软件安装操作题
以root用户身份,来操作安装
安装Apache的web服务器,设置服务端口为8081
yum list httpd
yum install httpd
sudo wget https://archive.apache.org/dist/apr/apr-1.7.0.tar.bz2
sudo tar -jxvf apr-1.7.0.tar.bz2
sudo mkdir /usr/local/apr
cd apr-1.7.0/
ls
./configure --prefix=/usr/local/apr
make
sudo apt install make
Listen 8081
二、nginx
Nginx 安装配置 | 菜鸟教程 (runoob.com)
三、Shell编程题
默认以root用户执行shell程序
1.
程序要求1:检查两个目录/var/wwww/html/jsp和/var/wwww/html/php是否存在。如果任何一级目录不存在,则创建该级目录。
程序要求2:在jsp和php目录中,各自对应创建index.jsp和index.php。将“hello world”各自写入到index.jsp和index.php文件中。
程序要求3:将路径/var/wwww/中的所有目录及文件,打包压缩生成data2021.bz2文件,该文件生成后,放在/root目录下。
#!/bin/bash
if [ ! -d /var/wwww/html/jsp ]; then
mkdir /var/wwww/html/jsp
fi
if [ ! -d /var/wwww/html/php ]; then
mkdir /var/wwww/html/php
fi
touch /var/wwww/html/jsp/index.jsp
touch /var/wwww/html/jsp/index.php
echo ”hello world” >> index.jsp
echo ”hello world” >> index.php
tar cvf data2021.bz2 /var/wwww/-C /root