毕竟不是运维,各种命令的参数通常是记不住的(其实也不用记忆),这里把测试过程中的常用命令简单记录下。
0x00、获取系统信息
# 发行版信息
cat /etc/redhat-release
cat /etc/issue
# 内核版本信息
uname -a
# 登录信息
cat /etc/motd
# CPU信息
cat /proc/cpuinfo
# 内存信息
cat /proc/meminfo
# 获取LAN ip
ifconfig | grep 'inet addr'
# 获取eth0的ip
ip addr show dev eth0 | awk '/inet/ {sub(///, " "); print $2}'
# path信息
echo -e ${PATH//:/n}
# 查看history,并筛选出最常用的10条命令
cat ~/.bash_history | sort | uniq -c | sort -rn | head -n 10
0x01、磁盘空间相关
# 查看当前目录下,各个目录占用磁盘大小
du -sh ./*
# 查看当前目录下,最大的十个文件/文件夹
du -sh * | sort -n | tail
0x02、目录、文件名相关
# 强制建立连续目录
mkdir -p /data01/joblog/si-data-servPROV
# 一次创建多个子目录
mkdir -p /root/work/{info,error,dubbo}
# 清理.DS_Store
find ./ -name .DS_Store -delete
# 给文件名加前缀
for i in *; do mv $i prependtext$i; done
0x02、文件内容相关
# 对tailf中出现的关键字高亮
tail -f info.2019-04-01.log | perl -pe 's/(info|INFO|error|ERROR|xception|错误)/e[1;31m$1e[0m/g'
# 每行尾部增加文本
sed 's/$/& ,旗舰店/g' tid.txt > tid.csv
# grep当前目录及子目录中文件
grep -r 'NumberFormatException.java' * --color
# 通过grep过滤出目标日志,包含时间戳,通过awk获取「时分」,之后通过统计,排序
zcat info.2018-09-19.log.gz | grep '调用天猫发货接口 |入参' | awk -F ":" '{print $1$2}' | uniq -c | sort -n
# 产生随机的十六进制数,n是字符数
openssl rand -hex <n>
# 获取文本的md5
echo -n "test" | md5sum
0x03、进程相关
# 看进程号为pid的进程打开了哪些文件
lsof -p <pid>
0x04、网络相关
# 用 wget 抓取完整的网站目录结构,存放到本地目录中
wget -r --no-parent --reject "index.html*" http://cfish.top/ -P /root/work
# 列出端口信息
lsof -i:<port>
# 按ip分组的netstat
netstat -ntu | awk ' $5 ~ /^[0-9]/ {print $5}' | cut -d: -f1 | sort | uniq -c | sort -n
0x10、日志打满磁盘
dubbo无法连接zk,猜测zk机器日志打满,导致socket连接建立失败。
# 查看磁盘空间占用
df -h
# 进入100%占用的挂载点
# 查看具体目录占用,发现zk目录和job目录占用很大
du -sh *
# 通过find查找大文件
find . -type f -size +500M
# 删除相关文件,但是文件句柄没有关闭,文件空间依然占用,重启相关进程,文件空间释放
ps -ef | grep -E '19067|24968|14776|486|20804|9202|24816'
0x11、提取使用开源软件列表
由于需要获取某软件使用开源软件列表,但是又不需要License的其他信息。
# 提取文本所需字符
grep 'Software: ' hw.txt | awk -F ': ' {'print $2'} | sort > rs.txt
0x12、生成SQL
需要在测试环境配置部分映射数据,原始JSON中包含映射ID以及关系,但是原始JSON为压缩后的JSON。
![98c2359bcf98b3d32beab2d8ff522bb3.png](https://i-blog.csdnimg.cn/blog_migrate/83a552b0f00ed4df20b871ed4b789af9.jpeg)
# 通过sed把JSON分行
# 使用egrep将关键行筛选
# 通过正则匹配关键内容
# 使用sed连接映射关系
cat big.json | sed 's/,/n/g' | egrep 'out_product_id|product_id' | grep -E -o '[1-9][0-9]{4,}' | sed 'N;s/n/,/'
# 使用SQL拼接工具,生成INSERT语句,批量插入映射关系数据
INSERT INTO `OuterUnionDB`.`product_pool` (`partner_id`, `outer_code`, `product_id`, `book_product_id`, `push_status`, `sale_status`, `remark`) VALUES ('29', ${f1}, ${f2}, '1', '1', '1', '测试品,请勿修改');