Linux技巧学习[ing]


监视磁盘空间使用量的shell脚本
#!/bin/bash
#monitor disk space usage amount
SPACE=`df | sed -n '//$/p' | awk '{print $5}' | sed 's/%//'`
if [ $SPACE -ge 90 ]
then
	echo "Alert!!! Disk space has been used more than $SPACE%" | mutt [email protected] -s "[disk space is not enough]"
fi
主机状态监控的脚本
#!/bin/bash
# os_status_log.sh
# 0 * * * * /home/useful_scripts/os_status_log.sh

DAT="`date +%Y%m%d`"
HOUR="`date +%H`"
DIR="/home/oslog/host_${DAT}/${HOUR}"
DELAY=60
COUNT=60

if ! test -d ${DIR}
then
	/bin/mkdir -p ${DIR}
fi

# general check
/usr/bin/top -b -d ${DELAY} -n ${COUNT} > ${DIR}/top_${DAT}.log 2>&1 &

# cpu check
/usr/bin/sar -u ${DELAY} ${COUNT} > ${DIR}/cpu_${DAT}.log 2>&1 &
#/usr/bin/mpstat -P 0 ${DELAY} ${COUNT} > ${DIR}/cpu_0_${DAT}.log 2>&1 &
#/usr/bin/mpstat -P 1 ${DELAY} ${COUNT} > ${DIR}/cpu_1_${DAT}.log 2>&1 &

# memory check
/usr/bin/vmstat ${DELAY} ${COUNT} > ${DIR}/vmstat_${DAT}.log 2>&1 &

# I/O check
/usr/bin/iostat ${DELAY} ${COUNT} > ${DIR}/iostat_${DAT}.log 2>&1 &

# network check
/usr/bin/sar -n DEV ${DELAY} ${COUNT} > ${DIR}/net_${DAT}.log 2>&1 &
#/usr/bin/sar -n EDEV ${DELAY} ${COUNT} > ${DIR}/net_edev_${DAT}.log 2>&1 &

 

用grep命令查找含有”-“字符的内容

# history | grep -a
Usage: grep [OPTION]… PATTERN [FILE]…
Try `grep –help’ for more information.

# history | grep ‘-a’
Usage: grep [OPTION]… PATTERN [FILE]…
Try `grep –help’ for more information.

# history | grep ‘-a’
2188 @2014-07-08_21:23:12 uname -a
2526 @2014-07-11_20:54:31 uname -a
2527 @2014-07-11_20:54:36 lsb_release -a
2697 @2014-07-13_18:30:52 ls -al
2698 @2014-07-13_18:31:01 ls -alh
2794 @2014-07-14_12:29:53 uname -a

需要将要查找的内容用引号引起来,然后使用反斜杠转义特殊字符。

用find命令查找近期修改过的文件

# find /root/ -mtime -1  #查找/root目录下在1天之内进行过修改的文件/文件夹
# find /home/ -mtime -1

查找大于10MB的所有文件

$ find / -size +10000k -xdev -exec ls –lh {} ;

查看当前使用的Shell类型

可以使用echo $0或ps -p $$命令显示您正在使用的Shell

重用以前的参数:

!$命令返回某个命令使用的上一个文件名参数。但如果某个命令使用多个文件名,而您只希望重用其中一个文件名,该如何做?!:1操作符返回某个命令使用的第一个文件名,!:2操作符返回某个命令使用的第二个文件名。实际测试如下:

# ll ccrypt_useage.txt download_blogbus__latest.sh
-rw-r--r-- 1 root root 2.4K Jun 22 01:41 ccrypt_useage.txt
-rwxr--r-- 1 root root 843 Jun 25 22:22 download_blogbus__latest.sh*
# ll !:1
ll ccrypt_useage.txt
-rw-r--r-- 1 root root 2.4K Jun 22 01:41 ccrypt_useage.txt
# ll ccrypt_useage.txt download_blogbus__latest.sh my.md5
-rw-r--r-- 1 root root 2.4K Jun 22 01:41 ccrypt_useage.txt
-rwxr--r-- 1 root root 843 Jun 25 22:22 download_blogbus__latest.sh*
-rw-r--r-- 1 root root 306 Jul 20 16:53 my.md5
# ll !:2
ll download_blogbus__latest.sh
-rwxr--r-- 1 root root 843 Jun 25 22:22 download_blogbus__latest.sh*

 

使用文本编辑器打开上一次执行的命令

$ fc
当fc命令执行后,会用文本编辑器打开上一个命令。当你想要编辑一个很长并且复杂的命令式,这个功能会帮你省下不少功夫。((默认的编辑器是Nano,如果你想设置编辑器为Vim你需要手动指定EDITOR变量的值)
注:设置默认的编辑器方法,例如 vim:export EDITOR=’vim’

你可以将其写入~/.bashrc中,从而不用每次都手动设置)

Bash终端小技巧

删除上一个单词

CTRL + w
删除一个单词也被称为”killing a word”,每个被删除的单词都被保存在缓存中,可以按下CTRL + y将其粘贴回来,这个操作被称为”yanking”。

正向历史搜索

CTRL + s
如果你按下CTRL + s,终端会停止屏幕刷新,因为默认情况下,你的终端将它解释成停止输出流的信号。当我是新手时,这种情况快把我逼疯了。每次我不小心按下CTRL + s后,屏幕就冻结了,然后我就不知道发生了什么。之后,我才学会用CTRL + q键来恢复终端(你可以用这种方法去逗一逗菜鸟`(*∩_∩*)′)

正确的方式应该是通过stty命令来更改终端对于CTRL + s按下后采取的行为:
$ stty stop ‘undef’
这样会取消默认的停止信号的快捷键绑定,然后你可以开始使用 Bash 的CTRL + s功能。
CTRL + s在 Bash 中的作用和CTRL + r相反,是执行正向历史搜索。

输入特殊字符

CTRL + v
按下CTRL + v之后,会取消下一个输入字符的特殊含义,例如CTRL + v后按下TAB键,可以在命令行下输入一个制表符,或者之后按下CTRL + m会输入一个 Windows 下的回车符(注: ^M)。

ngxtop的使用总结:

  • ngxtop info
  • ngxtop –no-follow
  • ngxtop –no-follow top remote_addr
  • ngxtop –no-follow -i ‘status==404’ print request status remote_addr

若你需要直接用终端,而不是依靠任何shell脚本来进行实数计算。你会怎么做(比如实数7.56+2.453)?
答案 : 我们需要用如下所述的特殊方式使用bc命令($ echo 7.56 + 2.453 | bc)将7.56+2.453作为输入通过管道进入bc中。

从WooYun知识库中看到的几个简单/有用的检测方法
find . -type f -name '*.php' -mtime -7  #查找当前目录下在7天之内进行过修改的PHP文件

find . -type f -name '*.php' | xargs grep -l "eval *(" --color	#(空格后面加上个*代表匹配任意个空格)
find . -type f -name '*.php' | xargs grep -l "base64_decode *(" --color
find . -type f -name '*.php' | xargs grep -l "gzinflate *(" --color
find . -type f -name '*.php' | xargs grep -l "eval *(str_rot13 *(base64_decode *(" --color

find . -type f -name '*.php' | xargs egrep -i "(mail|fsockopen|pfsockopen|stream_socket_client|exec|system|passthru|eval|base64_decode) *("

find . -type f -name '*.php' | xargs egrep -i "preg_replace *((['|"])(.).*2[a-z]*e[^1]*1 *," --color

diff -r wordpress-clean/ wordpress-compromised/ -x wp-content
#上面的例子是比较wordpress-clean/和wordpress-comprised/这两个目录,并且目录里面的wp-content/子目录不比较

#查看当前这个目录里面是否有可疑文件{查找权限为777的目录是否存在php文件}
search_dir=$(pwd)
writable_dirs=$(find $search_dir -type d -perm 0777)
for dir in $writable_dirs
    do
        #echo $dir
        find $dir -type f -name '*.php'
done

#黑客经常在jpg文件中插入php代码,因此在查询这些目录的时候也要查询jpg文件
find wp-content/uploads -type f -iname '*.jpg' | xargs grep -i php
#注意:-iname 表示文件名不区分大小写;grep -i 也表示查找内容不区分大小写

 


《“Linux技巧学习[ing]”》 有 1 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注