工作中常用的shell操作总结
1. 统计某个目录下的文件数量
#!/bin/bash
# count number of files in your PATH
my_path=`echo $PATH | sed 's/:/ /g'`
count=0
for directory in ${my_path}
do
check=`ls ${directory}`
echo ${check}
for item in ${check}
do
count=$[ $count + 1 ]
done
echo "$directory - $count"
count=0
done
2. 获取占用磁盘最大的几个文件
#!/bin/bash
CHECK_DIRECTORIES="/var/log /home"
DATE=$(date '+%m%d%y')
#Make report file Std Output
exec > disk_space_${DATE}.rpt
#Report header for while report
echo "十大磁盘空间使用情况"
echo "for $CHECK_DIRECTORIES Directories"
for DIR_CHECK in ${CHECK_DIRECTORIES}
do
echo ""
echo "The $DIR_CHECK Directory:"
du -S ${DIR_CHECK} 2>/dev/null |
sort -rn |
sed '{11,$D; =}' |
sed 'N; s/\n/ /' |
gawk '{printf $1 ":" "\t" $2 "\t" $3 "\n"}'
done
3. 获取磁盘占用率并进行预警
#! /bin/bash
requestId=$(cat /proc/sys/kernel/random/uuid)
echo ${requestId}
email_logfile="/data/ksyun/monitor/disk/diskdetail.txt";
if [[ ! -d "/data/ksyun/monitor/disk/" ]]; then
mkdir -p '/data/ksyun/monitor/disk/'
fi
if [[ ! -e "$email_logfile" ]]; then
touch "$email_logfile"
fi
cat /dev/null > ${email_logfile};
local_ip=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:");
receiver="email地址"
SendMail(){
ip=$1;
disk_percent=$2;
content='ip='${ip}',disk_percent='${disk_percent}'%';
#echo $content
contentStr=${content//\"/\\\"}
echo ${contentStr}
now_time=$(date "+%Y%m%d-%H:%M:%S")
subject='测试环境('${local_ip}')磁盘监控告警'${now_time};
curl --location --request POST 'http://公司发邮件地址/sendmail' \
--header 'Accept: application/json' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'mails='${receiver}'' \
--data-urlencode 'subject='${subject}'' \
--data-urlencode 'content='${contentStr}''
}
for d in `df -P | grep /dev | awk '{print $5}' | sed 's/%//g'`
do
if [[ ${d} -gt 80 ]]; then
df -h >>${email_logfile};
SendMail ${local_ip} ${d};
exit 0;
fi
done
4. 网络相关操作
#!/bin/sh
#查看http请求的header
tcpdump -s 1024 -l -A -n host 192.168.9.56
tcpdump -s 1024 -l -A src 192.168.9.56 or dst 192.168.9.56
sudo tcpdump -A -s 1492 dst port 80
#本地网络中IP地址为192.168.0.5的主机现在与外界通信的情况
sudo tcpdump -i eth0 src host 192.168.0.5
#查看http请求的request 包
sudo tcpdump -i eth0 port 80 -w -
#过滤http响应的get host头信息
sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E "GET \/.*|Host\: .*"
#DNS查询请求响应包
sudo tcpdump -i en0 'udp port 53'
#nmap -sP指定使用Ping echo 进行扫描 /24查看目的网络有多少主机处于运行状态
nmap -sP 192.168.0.1
nmap -sP 192.168.0.0/24
nmap -O www.baidu.com
#Netstat主要用于Linux/Unix主机察看自身的网络状况,如开启的端口、在为哪些用户服务以及服务的状态
netstat -a
netstat -nlp
#netcat扫描机器开放的端口
nc -z -v -n 172.31.100.7 21-25
#netcat 命令会连接开放端口21并且打印运行在这个端口上服务的banner信息。
nc -v 172.31.100.7 21
#输出网络的路由表
route
#显示结果包括服务器已经运行了多长时间,有多少登陆用户和对服务器性能的总体评估(load average)。
uptime
#查看mac地址
cat /sys/class/net/*/address
#查看eth0的ip
ifconfig eth0 | awk '/inet addr/ {split ($2,A,":"); print A[2]}'
#curl 查看页面对应的domail name
curl -s http://en.m.wikipedia.org/wiki/List_of_Internet_top-level_domains | sed -n '/<tr valign="top">/{s/<[^>]*>//g;p}'
#talnet
telnet localhost 6666
#查看所有的网络接口
awk '{print $1}' /proc/net/dev|grep :|sed "s/:.*//g"
#查看DNS server版本好
nslookup -q=txt -class=CHAOS version.bind NS.PHX5.NEARLYFREESPEECH.NET
5. 简单工具操作
#!/bin/bash
#查看uptime获取在线用户数.sh
uptime | sed 's/user.*$//' | gawk '{print $NF}'
#查看僵尸进程
ps -al | gawk '{print $2,$4}' | grep Z
#查看内存使用百分比
free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/'
#查看磁盘实用百分比
df -h /dev/vda1 | sed -n '/% \//p' | gawk '{ print $5 }'
#验证邮件
gawk --re-interval '/^([a-zA-Z0-9_\-\.\+]+)@([a-zA-Z0-9_\-\+]+)\.([a-zA-Z]{2,5})/{print $0}'
# 清除日志
cat /dev/null > [Logname]