shell脚本第四篇——常用小脚本

1、将系统进程按内存占用大小排列显示出来

# ps -e  -o %C   : %p : %z : %a|sort -k5-nr

 

2、将系统进程按CPU占用大小排列显示

# ps -e  -o %C   : %p : %z : %a|sort   -nr # ps aux --sort -rss

 

3、查找当前目录下占用为0字节的文件并删除

# find ./ -type f -size 0 -exec rm -rf {}\;    #此命令不要用于对根目录0字节文件的操作

 

4、匹配某文件中某一行并进行内容替换

#sed -i /Root/s/no/yes/etc/ssh/sshd_config #先匹配到Root,再将此行no替换为yes

 

5TCP抓包工具分析80端口数据流

# tcpdump -c 10000 -i eth0 -n dst port 80

 

6、删除所有空目录

# find /data -type d -empty -exec rm -rf {};    #最好不要在/目录下执行此命令

 

7、将来自80端口的请求转发到8080端口

#iptables -A PREROUTING -p tcp -m tcp dport80 -j DNAT to-destination 127.0.0.1:8080

 

8、通过apache访问日志access.log 统计IP及其访问的次数,按访问量列出前10

    # cat access.log | awk '{print $1}' |sort| uniq -c |sort -rn |head -10

 

9、锁定关键文件,防止被恶意篡改 || 解除锁定为chattr -i

# chattr +i /etc/passwd chattr +i /etc/inittab

 

10、更改文件描述符大小为最大值65535 下次登录生效,查看文件描述符值大小

# echo '* - nofile 65535' >>/etc/security/limits.conf # ulimit -n

 

11、通过ssh通道同步1.105/etc下的hosts文件到本机 /data目录下

# rsync -avz -e 'ssh -p 22' root@192.168.1.105:/etc/hosts /data/

 

12、杀掉80端口相关的进程

# lsof -i :80|grep -v "PID"|awk '{print "kill -9",$2}'|sh

 

13、取出/etc/passwd文件中shell出现的次数,shell指后面/bin/bash,/sbin/nologin

# cat /etc/passwd|awk -F: '{print $7}'|sort|uniq -c

 

14、在目录/tmp下找到100个以a开头的文件,然后把这些文件的第一行保存到文件file

     # find /tmp -type f -name “a*” | head -n 100 | xargs head -q -n 1 >> file

15、防攻击脚本,企业实用小脚本http://blog.csdn.net/linuxlsq/article/details/52606408