linux一些命令备忘

拒绝多次登录失败的人继续登录#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/bash
# -*- coding:UTF-8 -*-
#
#########################################################
# #
# file name: ssh_login_failed_host_deny #
# description: 将SSH多次登录失败的IP加入黑名单 #
#########################################################


# 通过lastb获取登录失败的IP及登录失败的次数
lastb | awk '{print $3}' | grep ^[0-9] | sort | uniq -c | awk '{print $1"\t"$2}' > /tmp/host_list
list=`cat /tmp/host_list`
line=`wc -l /tmp/host_list | awk '{print $1}'`
count=1

# 如果/tmp/host_list中有数据,循环至少需要执行一次
while [[ "$line" -ge "$count" ]]; do
ip_add=`echo $list | awk '{FS="\t"} {print $2}'`
num=`echo $list | awk '{FS="\t"} {print $1}'`
# 登录失败达到5次就将该IP写入文件
if [[ "$num" -ge 5 ]]; then
grep "$ip_add" /etc/hosts.deny &> /dev/null
if [[ "$?" -gt 0 ]]; then
# --------> 此处添加当前系统时间,请根据实际情况定义日期格式
echo "# $(date +%F' '%H:%M:%S)" >> /etc/hosts.deny
echo "sshd:$ip_add" >> /etc/hosts.deny
fi
fi
let count+=1
# 删除已经写入文件的IP
sed -i '1d' /tmp/host_list
# 修改$list变量
list=`cat /tmp/host_list`
done
# 清空临时文件
echo '' > /tmp/host_list
exit 0

逻辑删除,防止误操作#

先准备目录:mkdir -p /main/.rm_trash/files

下面文件rm.sh放到/main/.rm_trash/目录下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash
PARA_CNT=$#
# 逻辑删除存档地
TRASH_DIR="/main/.rm_trash/files"

for i in $*; do
if [[ "$i" == "-rf" ]];then
continue
elif [[ "$i" == "-r" ]];then
continue
fi
#echo "参数 $i"
STAMP=`date -d today +"%Y-%m-%d-%T"`
fileName=`basename $i`
first_char="${i:0:1}"
#echo $first_char
if [[ "$first_char" == "/" ]];then
echo "["$STAMP"] del $fileName from "$i >> $TRASH_DIR/../rm.log
else
echo "["$STAMP"] del $fileName from "`pwd`"/"$i >> $TRASH_DIR/../rm.log
fi
mv $i $TRASH_DIR/$fileName.$STAMP
done

然后在/etc/bash.bashrc中加上:

1
2
3
# 修改rm命令为mv命令
alias rm="bash /main/.rm_trash/rm.sh"

清理所有操作痕迹#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cat /dev/null > /var/log/wtmp
cat /dev/null > /var/log/btmp
cat /dev/null > /var/log/lastlog
cat /dev/null > /var/log/secure
cat /dev/null > /var/log/messages
cat /dev/null > /var/log/syslog
cat /dev/null > /var/log/tallylog
rm -rf /var/log/messages-*
rm -rf /var/log/secure-*
rm -rf /var/log/wtmp.*
rm -rf /var/log/syslog.*
rm -rf /var/log/kern.log.*
rm -rf /var/log/dpkg.log.*
rm -rf /var/log/auth.log.*
rm -rf /var/log/alternatives.log.*
history -c
history -w

一键安装v2#

1
2
3
bash <(curl -s -L https://git.io/v2ray.sh)

# https://raw.githubusercontent.com/233boy/v2ray/master/install.sh