文章目录
1、统计Apache的日志,按ip的访问次数排序
#!/bin/bash
awk '{ip[$1]++}END{for(i in ip){print i": "ip[i]}}' /etc/httpd/logs/access_log | sort -nr -k 2
效果:
2、搭建本地yum仓库
#!/bin/bash
# 自动搭建yum本地仓库
cd /etc/yum.repos.d/
if [ ! -d others ];then
mkdir others
fi
mv *.repo others/
echo "[centos7]
name=centos7
baseurl=file:///dvd
gpgcheck=0
enable=1
" > local.repo
if [ ! -d /dvd ];then
mkdir /dvd
fi
mount /dev/cdrom /dvd
3、批量修改文件名后缀
#!/bin/bash
# 需要跟2个位置参数,即文件所在的目录和文件后缀
destdir=$1
cd $destdir
for i in *
do
name=${i%.*} # 截取文件的名字
mv $i $name.$2
done
效果:
4、收集系统信息
#!/bin/bash
hostname=$(hostname)
ip=$(ifconfig eth0 | awk 'NR==2{print $2}')
sys_ver=$(cat /etc/redhat-release)
core_ver=$(uname -r)
total_mem=$(free -hm | awk 'NR==2{print $2}')
free_mem=$(free -hm | awk 'NR==2{print $4}')
free_gen=$(df -hT | awk '/\/$/{print $5}')
echo "主机名为:$hostname"
echo "ip地址为:$ip"
echo "系统版本为:$sys_ver"
echo "内核版本为:$core_ver"
echo "总内存为:$total_mem"
echo "剩余内存为:$free_mem"
echo "根分区剩余:$free_gen"
效果:
5、批量创建用户(利用文件)
#!/bin/bash
# 脚本说明: 需要在脚本所在的目录下创建useradd.txt文件,格式为 用户名:密码
# 一个用户占一行
count=$(wc -l ./useradd.txt| awk '{print $1}')
for i in `seq $count`
do
info=$(cat ./useradd.txt| awk "NR==$i{print $1}")
username=${info%:*}
password=${info#*:}
useradd $username
if [ $? -eq 0 ];then
echo $password | passwd --stdin $username
fi
done
效果:
6、批量删除用户(利用文件)
#!/bin/bash
# 脚本说明: 需要在脚本所在的目录下创建userdel.txt文件,格式为 用户名
# 一个用户占一行
count=$(wc -l ./userdel.txt| awk '{print $1}')
for i in `seq $count`
do
info=$(cat ./userdel.txt| awk "NR==$i{print}")
username=${info%:*}
userdel $username
done
效果:
7、初始化虚拟机
#!/bin/bash
hostnamectl set-hostname $1
nmcli connection modify ens33 ipv4.method manual ipv4.addresses 192.168.88.$2/24 connection.autoconnect yes
nmcli connection up ens33
mkdir /mydvd
echo "/dev/cdrom /mydvd iso9660 defaults 0 0" >> /etc/fstab
mount -a
cd /etc/yum.repos.d/
mkdir other
mv kylin_x86_64.repo other/
touch local.repo
echo "[local]
name=local
baseurl=file:///mydvd
gpgcheck=0
enabled=1
" > local.repo
yum repolist -v | tail -1