文章目录
一、显示系统中能被su命令切换的用户名称
#!/bin/bash
grep -E "bash$|sh$|tcsh$|csh$" /etc/passwd |cut -d : -f 1
二、修改httpd服务端口
编辑脚本Apache_port.sh:
此脚本后介入数字,http的端口就改为此数字;假设selinux为关闭状态
#!/bin/bash
[ -z "$1" ] && {
echo "Error:no port.please input the port number for Apache!"
exit
}
# 检测脚本后面是否输入数字
[ -z "`netstat -antlupe | grep $1`" ] || {
echo "Error:$1 is used by system proto!"
exit
}
# 检测想要修改的端口是否被占用
[ -e "/etc/httpd/conf/httpd.conf" ] || {
yum install httpd.x86_64 -y &>/dev/null || {
echo "Error:apache not installed and yum repo is not available"
exit
}
}
#检测httpd配置文件是否存在,是否安装httpd服务
sed "/^Listen/c Listen $1" -i /etc/httpd/conf/httpd.conf #修改端口并保存文件
firewall-cmd --permanent --add-port=$1/tcp &>/dev/null
firewall-cmd --reload &>/dev/null #在火墙中添加端口并刷新设定
systemctl restart httpd &>/dev/null && {
echo Now the Apache port number has changed to $1 .
} || {
echo "Error: can't up httpd service!See "journalctl -xe" for details."
}
#检测重启httpd服务是否成功
三、统计在系统中能su切换的并且用户家目录不在/home下的用户数量
#!/bin/bash
awk -F : '$6!~/home/&&/bash$/{print}' /etc/passwd | wc -l
四、输入网卡名称,可以显示当前主机名称
编辑脚本ip_show.sh,调用时,加入网卡名称,可以显示当前主机名称
#!/bin/bash
[ -z $1 ] && {
echo "Pleace write the narwork card name"
exit
}
# 检测是否输入网卡名称
ifconfig $1 &>/dev/null || {
echo "Error:interface $1 is not found"
exit
}
# 检测输入的网卡名称是否存在
hostname | awk -F . '{print $1}' #网卡名称正确,执行命令
五、显示当前主机的名称,IP,登录当前主机的用户
编辑脚本host_message.sh可以显示:
hostname:xxxxxx
ipaddress:xxx.xxx.xxx
username:xxxxxx
#!/bin/bash
echo "hostname:"
hostname | awk -F . '{print $1}'
echo "ipaddress:"
ifconfig ens160 | awk '/inet\>/{print $2}'
echo "username:"
whoami
六,清空日志
编辑脚本clear_log.sh,可以清空日志
#!/bin/bash
[ "`whoami`" = "root" ] || {
echo "Error: This script must run as root"
exit
}
#检测当前用户是否为超级用户,只有超级用户才有权力清空日志
for Log_Name in `awk '/./&&!/^#/&&!/"/&&$2~/^\/|^\-/{print $2}' /etc/rsyslog.conf | sed 's/-//g'`
do
> $Log_Name && {
echo $Log_Name is cleared.
}
done