学员命令测试: 1.ifconfig 网卡 可以显示此网卡的信息 ? 显示信息中包含此网卡使用的ip地址 ? 请用命令过滤此ip并在输出时只显示ip其他信息不显示
[root@westosa mnt]# ifconfig ens3
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.254.100 netmask 255.255.255.0 broadcast 172.25.254.255
inet6 fe80::59cc:1b48:97f1:83c prefixlen 64 scopeid 0x20<link>
ether 52:54:00:00:12:12 txqueuelen 1000 (Ethernet)
RX packets 4259 bytes 8991531 (8.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2530 bytes 372338 (363.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@westosa mnt]# ifconfig ens3 | head -n 2 | tail -n 1 | cut -d ' ' -f 10
172.25.254.100
2.找出能登陆系统用户中UID最大的用户,并显示其名称
[root@westosa mnt]# grep bash$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
westos:x:1000:1000:westos:/home/westos:/bin/bash
[root@westosa mnt]# grep bash$ /etc/passwd | sort -t : -k 3 -nr | head -n 1 | cut -d : -f 1
westos
小实验:判断数字范围
[root@westosa mnt]# vim num_check.sh
[root@westosa mnt]# sh num_check.sh 10
10 is not in 0-9
[root@westosa mnt]# sh num_check.sh 9
9 is in 0-9
[root@westosa mnt]# cat num_check.sh
#!bin/bash
[ "$1" -ge "0" -a "$1" -lt "10" ] && {
echo $1 is in 0-9
}||{
echo $1 is not in 0-9
}
学员检测 编写脚本完成以下条件 file_check.sh 在执行时 如果脚本后未指定检测文件报错“未指定检测文件,请指定” 如果脚本后指定文件不存在报错“此文件不存在” 当文件存在时请检测文件类型并显示到输出中
[root@westosa mnt]# vim file_check.sh
//写脚本
[ -z "$1" ] && {
echo "未指定检测文件,请指定!"
exit
}
[ -o "$1" ] || {
echo "此文件不存在"
exit
}
[ -d "$1" ] && {
echo "文件类型为目录"
exit
}
[ -S "$1" ] && {
echo "文件类型为套接字"
exit
}
[ -L "$1" ] && {
echo "文件类型为软链接"
exit
}
[ -f "$1" ] && {
echo "文件类型为普通文件"
exit
}
[ -b "$1" ] && {
echo "文件类型为块设备"
exit
}
[ -c "$1" ] && {
echo "文件类型为字符设备"
exit
//
练习脚本: 请显示系统中能被su命令切换的用户名称
[root@westosa mnt]# grep -E "bash$|sh$" /etc/passwd | cut -d : -f 1
root
练习及脚本 Apache_port.sh 此脚本接入数字 http的端口就改为此数字 假设selinux为关闭状态 例如: sh Apache_port.sh ERROR: Pleaase input port number following script !!
sh Apache_port.sh 8080 apache的端口会被修改为8080
vim Apache_port.sh
//编写内容
#!/bin/bash
setenforce 0 &> /dev/null
[ -z "$1" ] && {
echo "Error:Please input port following script !!"
exit
}
rpm -q httpd &> /dev/null || {
echo "Error:Apache is not installed !!"
exit
}
systemctl status httpd | grep "running" &> /dev/null || {
echo "Error:Apache is not running !!"
exit
}
netstat -antlupe | grep -E "$1\>" &> /dev/null && {
echo "Error:$1 is in used !!"
exit
}
sed "/^Listen/cListen $1" -i /etc/httpd/conf/httpd.conf
systemctl restart httpd
netstat -antlupe | grep http
//
测试:
[root@localhost mnt]# sh Apache_port.sh
Error:Please input port following script !!
[root@localhost mnt]# sh Apache_port.sh 8080
tcp6 0 0 :::8080 :::* LISTEN 0 171157 7664/httpd
课后练习: 统计在系统中能su切换的并且用户加目录不在/home下的用户数量
思路:使用awk命令以:为分隔符截取/etc/passwd文件中第六列不以/home开头的(/需要转译;不能直接写不含有home关键字,存在类似用户家目录为/tmp/home这种情况)、并且以bash或sh结尾的行,对截取结果进行计数,这里我们可以创建用户对脚本进行测试
[root@westoslinux mnt]# vim su.sh
[root@westoslinux mnt]# cat su.sh
awk -F : 'BEGIN{N=0}$6!~/^\/home/&&/bash$|sh$/{N++}END{print N}' /etc/passwd
[root@westoslinux mnt]# useradd -d /tmp/home lee
useradd: cannot set SELinux context for home directory /tmp/home
[root@westoslinux mnt]# sh su.sh
2
[root@westoslinux mnt]# userdel -r lee
userdel: lee mail spool (/var/spool/mail/lee) not found
userdel: lee home directory (/tmp/home) not found
[root@westoslinux mnt]# sh su.sh
1
脚本练习:?? ip_show.sh 网卡?? ?显示当前的IP
vim /mnt/test.sh
//
#!/bin/bash
[ -z "$1" ] && {
echo "Error: Please input interface following script !!"
exit
}
ifconfig $1 | awk '/\<inet\>/{print $2}'
//
测试:
[root@westoslinux mnt]# sh ip_show.sh ens3
172.25.254.100
[root@westoslinux mnt]# sh ip_show.sh ens10
[root@westoslinux mnt]# sh ip_show.sh westos
westos: error fetching interface information: Device not found
host_messages.sh 显示当前主机的名称,ip登陆当前主机的用户
hostname:?? ?xxxxx ipaddress:?? ?xxxx.xxxx.xxx.xxx username:?? ?root
[root@westoslinux mnt]# vim host_messages.sh
[root@westoslinux mnt]# cat host_messages.sh
#!/bin/bash
echo "hostname: `hostname`"
echo "ipaddress: `ifconfig ens3|awk '/inet\>/{print $2}'`"
echo "username: $USER"
[root@westoslinux mnt]# sh host_messages.sh
hostname: westoslinux.westos.org
ipaddress: 172.25.254.100
username: root
clear_log.sh?? ?执行次脚本后可以清空日志
[root@westoslinux mnt]# vim clear_log.sh
[root@westoslinux mnt]# sh clear_log.sh
/var/log/messages is cleaned !!
[root@westoslinux mnt]# cat clear_log.sh
#!/bin/bash
[ "$USER" != "root" ] && {
echo "Error: Please run script with root !!"
exit
}
[ ! -e "/var/log/messages" ] && {
echo "Error: not found log file "
exit
}
> /var/log/messages && {
echo /var/log/messages is cleaned !!
}
# 练习脚本 # sh create_user.sh Please input username: westos westos exist<output>> westos is exist>Please input username: westos not existplease input password: 无回显密码 此用户会自动建立并且密码为提示后设定的密码 并显示:westos is created 并再次提示Please input username: 当Please input username:exit 此脚本退出
[root@westoslinux ~]# cat create_user.sh
###########################
#Author: lee
#Version: 1.0
#Create_Time: 2021/08/14
#Mail: lee@westos.org
#Info:
#
#
###########################
#!/bin/bash
CREATE()
{
read -p "Please input username:" NAME
[ "$NAME" = "exit" ] && {
exit
}
id $NAME &> /dev/null && {
echo ""
echo "$NAME is exist"
echo ""
CREATE
}||{
echo ""
read -p "Please input password:" -s PASSWD
useradd $NAME
echo $PASSWD | passwd --stdin $NAME
echo ""
echo "$NAME is created"
CREATE
}
}
CREATE
测试:
[root@westoslinux ~]# vim create_user.sh
[root@westoslinux ~]# sh create_user.sh
Please input username:westos
westos is exist
Please input username:lee
Please input password:1Changing password for user lee.
passwd: all authentication tokens updated successfully.
lee is created
Please input username:lee
Please input password:Changing password for user 1lee.
passwd: all authentication tokens updated successfully.
1lee is created
Please input username:exit
|