├── 01_parted_mkfs
├── 1_parted.sh
├── 2_mkfs.sh
├── 3_mkdir.sh
└── 4_mount.sh
├── 02_raid_monitor
└── raid_monitor.sh
├── 03_keepalived_check
├── README.md
└── keepalived_check.py
├── 04_linux_iptables
└── linux_drop_port.sh
├── 05_change_hostname
└── change_hostname_centos.sh
├── 06_denyhosts
├── README.md
└── denyhosts.sh
├── 07_audit_action
├── README.md
└── audit_action.sh
├── 08_rm
├── README.md
└── rmtrash.sh
├── 09_screen
├── README.md
└── screen.sh
├── 10_rebuild_centos
└── rebuild_centos6.x_X64_image.sh
├── 11_shell_common
├── README.md
├── scan_disk
│ └── scan_disk.sh
├── shell_processbar
│ ├── ProcessBash1.sh
│ └── ProcessBash2.sh
├── shell_whiptail
│ ├── checklist.sh
│ ├── input-box.sh
│ ├── menu-box.sh
│ ├── password-box.sh
│ ├── progress-bar.sh
│ ├── radiolist.sh
│ ├── yes-no.sh
│ └── yes-no2.sh
└── trap_err
│ ├── README.md
│ └── trap_err.sh
├── 12_blogger
├── README.md
├── b_lib
│ └── log.sh
└── test.sh
├── 13_mount_file
├── README.md
└── mount_file.sh
├── 14_daemon
├── README.md
└── run.sh
├── 15_tcpcopy
├── README.md
├── run.sh
└── tcp_copy
│ ├── cept.sh
│ └── copy.sh
├── 16_truncate
└── README.md
├── 17_logrotate
├── README.md
├── log_control.sh
├── log_rotate_lib
│ ├── log.sh
│ └── op_log_rotate.sh
├── logrotate_exe.sh
└── logrotate_loop.sh
├── 18_opmv
└── opmv
├── LICENSE
└── README.md
/01_parted_mkfs/1_parted.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | PATH=/bin:/sbin:/usr/bin:/usr/sbin
3 | i=1
4 | while [ $i -lt 13 ]
5 | do
6 | j=`echo $i|awk '{printf "%c",97+$i}'`
7 | echo $j
8 | parted /dev/sd$j << End
9 | mklabel gpt
10 | mkpart primary 0% 100%
11 | quit
12 | End
13 | let i+=1
14 | done
15 |
--------------------------------------------------------------------------------
/01_parted_mkfs/2_mkfs.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | PATH=/bin:/sbin:/usr/bin:/usr/sbin
3 | i=1
4 | while [ $i -lt 13 ]
5 | do
6 | j=`echo $i|awk '{printf "%c",97+$i}'`
7 | echo $j
8 | mkfs.ext4 /dev/sd${j}1 &
9 | let i+=1
10 | done
11 |
--------------------------------------------------------------------------------
/01_parted_mkfs/3_mkdir.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | PATH=/bin:/sbin:/usr/bin:/usr/sbin
3 | i=1
4 | while [ $i -lt 13 ]
5 | do
6 | j=`echo $i|awk '{printf "%c",97+$i}'`
7 | echo $j
8 | mkdir -p /mnt/sd$j
9 | let i+=1
10 | done
11 |
--------------------------------------------------------------------------------
/01_parted_mkfs/4_mount.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | PATH=/bin:/sbin:/usr/bin:/usr/sbin
3 | i=1
4 | while [ $i -lt 13 ]
5 | do
6 | j=`echo $i|awk '{printf "%c",97+$i}'`
7 | echo $j
8 | mount /dev/sd${j}1 /mnt/sd$j
9 | let i+=1
10 | done
11 |
--------------------------------------------------------------------------------
/02_raid_monitor/raid_monitor.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | #check raid disk status
3 |
4 | MEGACLI="sudo /opt/MegaRAID/MegaCli/MegaCli64 "
5 |
6 | $MEGACLI -pdlist -aALL -NoLog | grep "Firmware state" | awk -F : '{print $2}' | awk -F , '{print $1}' >/tmp/fireware.log
7 | $MEGACLI -pdlist -aALL -NoLog | grep -E "Media Error|Other Error" | awk -F : '{print $2}' >/tmp/disk.log
8 |
9 | for i in `cat < /tmp/disk.log`
10 | do
11 | if [ $i -ne 0 ];then
12 | echo "raid_disk_error"
13 | fi
14 | done
15 |
16 | for i in `cat < /tmp/fireware.log`
17 | do
18 | if [ $i != Online ];then
19 | echo "raid_disk_offline"
20 | fi
21 | done
22 |
--------------------------------------------------------------------------------
/03_keepalived_check/README.md:
--------------------------------------------------------------------------------
1 | # keepalived-tools
2 | ## keepalived_checker.py
3 | ### Description
4 | Check duplications or typo of VRRP IDs (vrid), Virtual IP Addresses (vrip) and Virtual Servers (vs) from 'keepalived.conf'.
5 |
6 | ### Tested on
7 | - CentOS 6.5
8 | - keepalived-1.2.13
9 |
10 | ### Required
11 | - Python 2.6 or 2.7
12 |
13 | ### Usage
14 | Simply, run it.
15 |
16 | ```
17 | $ ./keepalived_checker.py
18 | ```
19 |
20 | If your config file is located on non default path, add `-f`.
21 |
22 | ```
23 | $ ./keepalived_checker.py -f CONF_PATH
24 | ```
25 |
26 | ### Output Examples
27 | You will get output like this if NG has found.
28 |
29 | ```
30 | $ ./keepalived_checker.py
31 | 'virtual_server' duplications found:
32 | 192.168.1.1:80
33 | - /etc/keepalived/keepalived.conf:20
34 | - /etc/keepalived/conf.d/test.conf:2
35 | ```
36 |
37 | If no errors found, get this.
38 |
39 | ```
40 | $ ./keepalived_checker.py
41 | OK
42 | ```
43 |
--------------------------------------------------------------------------------
/03_keepalived_check/keepalived_check.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding=utf8
3 | """
4 | # Author: Bill
5 | # Created Time : 2016年05月28日 星期六 12时22分00秒
6 |
7 | # File Name: keepalived_check.py
8 | # Description:
9 |
10 | """
11 | import os, sys, re
12 | import glob
13 | import optparse
14 | version = "0.1.0"
15 |
16 | def_conf_path = '/etc/keepalived/keepalived.conf'
17 |
18 | regex_confline = re.compile(r'''^(?P[^!#]+)(.*)$''', flags=re.IGNORECASE)
19 | regex_include = re.compile(r'''^\s*include\s+(?P[^\s]+).*$''', flags=re.IGNORECASE)
20 | # config regex
21 | regex_vrid = re.compile(r'''^\s*virtual_router_id\s+(?P\d+).*$''', flags=re.IGNORECASE)
22 | regex_vip = re.compile(r'''^\s*(?P(\d{1,3}\.){3}\d{1,3}).*$''', flags=re.IGNORECASE)
23 | regex_vs = re.compile(r'''^\s*virtual_server\s+(?P(\d{1,3}\.){3}\d{1,3})\s+(?P\d+).*$''', flags=re.IGNORECASE)
24 |
25 |
26 | def read_config(path=""):
27 | '''
28 | read_config read configs with support include stetement,
29 | and remove comments or blank lines.
30 | returns:
31 | list of tupple(parameter, filename:index)
32 | '''
33 | conf_dir = os.path.dirname(path)
34 |
35 | try:
36 | config = list()
37 | num = 0
38 | for line in open(path):
39 | num += 1
40 | m = regex_confline.match(line)
41 | if m is None :
42 | continue
43 | ### parse
44 | param = m.group('param').rstrip()
45 | m_include = regex_include.match(param)
46 | if m_include :
47 | include_path = m_include.group('path')
48 | for p in glob.glob('/'.join([conf_dir, include_path])):
49 | config.extend(read_config(p))
50 | else :
51 | index = "%s:%i" % (path, num)
52 | config.append((param, index))
53 |
54 | return config
55 | except:
56 | raise IOError("conffile '%s' not found" % path)
57 |
58 | def parse_config(config=[]):
59 | vrids = list()
60 | vips = list()
61 | virtual_servers = list()
62 |
63 | for line, index in config:
64 | # vrid
65 | m = regex_vrid.match(line)
66 | if m :
67 | vrids.append((m.group('vrid'), index))
68 | continue
69 | # virtual_server
70 | m = regex_vs.match(line)
71 | if m :
72 | virtual_servers.append(((m.group('vip'),m.group('port')), index))
73 | continue
74 | # vip
75 | m = regex_vip.match(line)
76 | if m :
77 | vips.append((m.group('vip'), index))
78 | continue
79 |
80 | return vrids, vips, virtual_servers
81 |
82 |
83 | def check_vrids(vrids):
84 | dups = __check_vrids_dup(vrids)
85 | return len(dups) == 0
86 |
87 | def __check_vrids_dup(vrids):
88 | vrid_list = list( map(lambda x: x[0], vrids) )
89 | unique_list = list(set( map(lambda x: x[0], vrids) ))
90 |
91 | for ele in unique_list:
92 | vrid_list.remove(ele)
93 |
94 | if len(vrid_list) > 0 :
95 | print("'virtual_router_id' duplications found:")
96 | for ele in vrid_list:
97 | print("\t" + ele)
98 | for vrid, index in vrids:
99 | if vrid == ele :
100 | print("\t\t- %s" % index)
101 | print
102 | return vrid_list
103 |
104 |
105 |
106 | def check_vips(vips, virtual_servers):
107 | dups_vip = __check_vips_dup(vips)
108 | dups_vs = __check_vs_dup(virtual_servers)
109 | ng_vips = __check_vips_unmanaged(vips, virtual_servers)
110 | if (len(dups_vip) + len(dups_vs) + len(ng_vips)) == 0 :
111 | return True
112 | else:
113 | return False
114 |
115 | def __check_vips_dup(vips):
116 | vip_list = map(lambda x: x[0], vips)
117 | unique_list = list(set(vip_list))
118 |
119 | for ele in unique_list:
120 | vip_list.remove(ele)
121 |
122 | if len(vip_list) > 0 :
123 | print("'virtual_ipaddress' duplications found:")
124 | for ele in vip_list:
125 | print("\t" + ele)
126 | for vip, index in vips:
127 | if vip == ele :
128 | print("\t\t- %s" % index)
129 | print
130 |
131 | return vip_list
132 |
133 | def __check_vs_dup(virtual_servers):
134 | vs_list = map(lambda x: x[0], virtual_servers)
135 | unique_list = list(set(vs_list))
136 |
137 | for ele in unique_list:
138 | vs_list.remove(ele)
139 |
140 | if len(vs_list) > 0 :
141 | print("'virtual_server' duplications found:")
142 | for ele in vs_list:
143 | print("\t" + ':'.join(ele))
144 | for vs, index in virtual_servers:
145 | if vs == ele :
146 | print("\t\t- %s" % index)
147 | print
148 |
149 | return vs_list
150 |
151 |
152 | def __check_vips_unmanaged(vips, virtual_servers):
153 | managed_list = map(lambda x: x[0], vips)
154 | unmanaged_list = list()
155 |
156 | for (vip, port) in map(lambda x: x[0], virtual_servers):
157 | if vip not in managed_list :
158 | unmanaged_list.append((vip, port))
159 |
160 | if len(unmanaged_list) > 0 :
161 | print("'virtual_server' uses unmanaged VIP:")
162 | for ele in unmanaged_list:
163 | print("\t" + ':'.join(ele))
164 | for vs, index in virtual_servers:
165 | if vs == ele :
166 | print("\t\t- %s" % index)
167 | print
168 |
169 | return unmanaged_list
170 |
171 |
172 |
173 | if __name__ == "__main__":
174 | import optparse
175 | usage = """usage: %prog [options]"""
176 |
177 | parser = optparse.OptionParser(usage=usage, version=version)
178 | parser.add_option(
179 | "-f", "--file",
180 | action="store",
181 | dest="conf_path",
182 | default=def_conf_path,
183 | help="set keepalived config file path. (default:%s)" % def_conf_path
184 | )
185 | (options, args) = parser.parse_args()
186 | if len(args) != 0 :
187 | parser.print_help()
188 | sys.exit(3)
189 |
190 | config = read_config(options.conf_path)
191 | vrids, vips, virtual_servers = parse_config(config)
192 |
193 | ret = 0
194 | if check_vrids(vrids) != True :
195 | ret = 1
196 | if check_vips(vips, virtual_servers) != True :
197 | ret = 1
198 |
199 | if ret == 0 :
200 | print("OK")
201 | sys.exit(ret)
202 |
--------------------------------------------------------------------------------
/04_linux_iptables/linux_drop_port.sh:
--------------------------------------------------------------------------------
1 | #########################################################################
2 | # File Name: linux_drop_port.sh
3 | # Author: Bill
4 | # mail: XXXXXXX@qq.com
5 | # Created Time: 2016-06-02 22:57:25
6 | #########################################################################
7 | #!/bin/bash
8 |
9 | #check_os_release{{{1
10 | check_os_release()
11 | {
12 | while true
13 | do
14 | os_release=$(grep "Red Hat Enterprise Linux Server release" /etc/issue 2>/dev/null)
15 | os_release_2=$(grep "Red Hat Enterprise Linux Server release" /etc/redhat-release 2>/dev/null)
16 | if [ "$os_release" ] && [ "$os_release_2" ]
17 | then
18 | if echo "$os_release"|grep "release 5" >/dev/null 2>&1
19 | then
20 | os_release=redhat5
21 | echo "$os_release"
22 | elif echo "$os_release"|grep "release 6" >/dev/null 2>&1
23 | then
24 | os_release=redhat6
25 | echo "$os_release"
26 | else
27 | os_release=""
28 | echo "$os_release"
29 | fi
30 | break
31 | fi
32 | os_release=$(grep "Aliyun Linux release" /etc/issue 2>/dev/null)
33 | os_release_2=$(grep "Aliyun Linux release" /etc/aliyun-release 2>/dev/null)
34 | if [ "$os_release" ] && [ "$os_release_2" ]
35 | then
36 | if echo "$os_release"|grep "release 5" >/dev/null 2>&1
37 | then
38 | os_release=aliyun5
39 | echo "$os_release"
40 | elif echo "$os_release"|grep "release 6" >/dev/null 2>&1
41 | then
42 | os_release=aliyun6
43 | echo "$os_release"
44 | else
45 | os_release=""
46 | echo "$os_release"
47 | fi
48 | break
49 | fi
50 | os_release=$(grep "CentOS release" /etc/issue 2>/dev/null)
51 | os_release_2=$(grep "CentOS release" /etc/*release 2>/dev/null)
52 | if [ "$os_release" ] && [ "$os_release_2" ]
53 | then
54 | if echo "$os_release"|grep "release 5" >/dev/null 2>&1
55 | then
56 | os_release=centos5
57 | echo "$os_release"
58 | elif echo "$os_release"|grep "release 6" >/dev/null 2>&1
59 | then
60 | os_release=centos6
61 | echo "$os_release"
62 | else
63 | os_release=""
64 | echo "$os_release"
65 | fi
66 | break
67 | fi
68 | os_release=$(grep -i "ubuntu" /etc/issue 2>/dev/null)
69 | os_release_2=$(grep -i "ubuntu" /etc/lsb-release 2>/dev/null)
70 | if [ "$os_release" ] && [ "$os_release_2" ]
71 | then
72 | if echo "$os_release"|grep "Ubuntu 10" >/dev/null 2>&1
73 | then
74 | os_release=ubuntu10
75 | echo "$os_release"
76 | elif echo "$os_release"|grep "Ubuntu 12.04" >/dev/null 2>&1
77 | then
78 | os_release=ubuntu1204
79 | echo "$os_release"
80 | elif echo "$os_release"|grep "Ubuntu 12.10" >/dev/null 2>&1
81 | then
82 | os_release=ubuntu1210
83 | echo "$os_release"
84 | else
85 | os_release=""
86 | echo "$os_release"
87 | fi
88 | break
89 | fi
90 | os_release=$(grep -i "debian" /etc/issue 2>/dev/null)
91 | os_release_2=$(grep -i "debian" /proc/version 2>/dev/null)
92 | if [ "$os_release" ] && [ "$os_release_2" ]
93 | then
94 | if echo "$os_release"|grep "Linux 6" >/dev/null 2>&1
95 | then
96 | os_release=debian6
97 | echo "$os_release"
98 | else
99 | os_release=""
100 | echo "$os_release"
101 | fi
102 | break
103 | fi
104 | os_release=$(grep "openSUSE" /etc/issue 2>/dev/null)
105 | os_release_2=$(grep "openSUSE" /etc/*release 2>/dev/null)
106 | if [ "$os_release" ] && [ "$os_release_2" ]
107 | then
108 | if echo "$os_release"|grep "13.1" >/dev/null 2>&1
109 | then
110 | os_release=opensuse131
111 | echo "$os_release"
112 | else
113 | os_release=""
114 | echo "$os_release"
115 | fi
116 | break
117 | fi
118 | break
119 | done
120 | }
121 | #exit_script{{{1
122 | exit_script()
123 | {
124 | echo -e "\033[1;40;31mInstall $1 error,will exit.\n\033[0m"
125 | rm -f $LOCKfile
126 | exit 1
127 | }
128 |
129 | #config_iptables{{{1
130 | config_iptables()
131 | {
132 | iptables -I OUTPUT 1 -p tcp -m multiport --dport 21,22,23,25,53,80,135,139,443,445 -j DROP
133 | iptables -I OUTPUT 2 -p tcp -m multiport --dport 1433,1314,1521,2222,3306,3433,3389,4899,8080,18186 -j DROP
134 | iptables -I OUTPUT 3 -p udp -j DROP
135 | iptables -nvL
136 | }
137 | #ubuntu_config_ufw{{{1
138 | ubuntu_config_ufw()
139 | {
140 | ufw deny out proto tcp to any port 21,22,23,25,53,80,135,139,443,445
141 | ufw deny out proto tcp to any port 1433,1314,1521,2222,3306,3433,3389,4899,8080,18186
142 | ufw deny out proto udp to any
143 | ufw status
144 | }
145 |
146 | #}}}
147 | ####################Start###################
148 | #check lock file ,one time only let the script run one time
149 | LOCKfile=/tmp/.$(basename $0)
150 | if [ -f "$LOCKfile" ]
151 | then
152 | echo -e "\033[1;40;31mThe script is already exist,please next time to run this script.\n\033[0m"
153 | exit
154 | else
155 | echo -e "\033[40;32mStep 1.No lock file,begin to create lock file and continue.\n\033[40;37m"
156 | touch $LOCKfile
157 | fi
158 |
159 | #check user
160 | if [ $(id -u) != "0" ]
161 | then
162 | echo -e "\033[1;40;31mError: You must be root to run this script, please use root to execute this script.\n\033[0m"
163 | rm -f $LOCKfile
164 | exit 1
165 | fi
166 |
167 | echo -e "\033[40;32mStep 2.Begen to check the OS issue.\n\033[40;37m"
168 | os_release=$(check_os_release)
169 | if [ "X$os_release" == "X" ]
170 | then
171 | echo -e "\033[1;40;31mThe OS does not identify,So this script is not executede.\n\033[0m"
172 | rm -f $LOCKfile
173 | exit 0
174 | else
175 | echo -e "\033[40;32mThis OS is $os_release.\n\033[40;37m"
176 | fi
177 |
178 | echo -e "\033[40;32mStep 3.Begen to config firewall.\n\033[40;37m"
179 | case "$os_release" in
180 | redhat5|centos5|redhat6|centos6|aliyun5|aliyun6)
181 | service iptables start
182 | config_iptables
183 | ;;
184 | debian6)
185 | config_iptables
186 | ;;
187 | ubuntu10|ubuntu1204|ubuntu1210)
188 | ufw enable <
5 | * [ssh暴力破解检测方法](#ssh暴力破解检测方法)
6 | * [ssh防暴力破解方法](#ssh防暴力破解方法)
7 | * [提升ssh安全](#提升ssh安全)
8 | * [修改sshd服务器的配置文件/etc/ssh/sshd_config](#修改sshd服务器的配置文件/etc/ssh/sshd_config)
9 | * [修改sshd服务器的配置文件/etc/ssh/sshd_config的读写权限,](#修改sshd服务器的配置文件/etc/ssh/sshd_config的读写权限,)
10 | * [设置TCP Wrappers](#设置tcp-wrappers)
11 | * [尽量关闭一些系统不需要的启动服务](#尽量关闭一些系统不需要的启动服务)
12 | * [其他](#其他)
13 |
14 |
15 |
16 | ### ssh暴力破解检测方法
17 |
18 | 输出尝试密码失败IP列表
19 | ```
20 | # cat /var/log/secure | awk '/Failed/{print $(NF-3)}' | sort | uniq -c | awk '{print $2" = "$1;}'| sort -n -k 2 -t = -r
21 | ```
22 |
23 | ### ssh防暴力破解方法
24 |
25 | (1)简单脚本操作
26 |
27 | 原理就是定时检查/var/log/secure中尝试密码登陆IP,超过10次后,将此IP放到/etc/hosts.deny中,禁止ssh登陆
28 |
29 | 通过crontab来执行,每天的1点0分执行一次。
30 |
31 | 0 1 * * * sh /root/bin/denyhosts.sh
32 |
33 | 下载方式
34 | ```
35 | #curl -o denyhosts.sh https://raw.githubusercontent.com/BillWang139967/linux_tools/master/06_denyhosts/denyhosts.sh
36 | ```
37 |
38 | (2)DenyHosts
39 |
40 | 据说有点坑,就不详细介绍了
41 |
42 | ## 提升ssh安全
43 |
44 | ### 修改sshd服务器的配置文件/etc/ssh/sshd_config
45 |
46 | * Port 5555 #系统缺省使用22号端口
47 | * ListenAddress 192.168.0.1 #设定sshd只在其中一个指定的接口地址监听,这样可以减少sshd的入口,降低入侵的可能性。
48 | * PermitRootLogin no #禁止root用户登录
49 | * PermitEmptyPasswords no #禁止空密码登陆
50 | * Protocol 2 #禁止使用版本1协议,因为其存在设计缺陷,很容易使密码被黑掉。
51 |
52 | ### 修改sshd服务器的配置文件/etc/ssh/sshd_config的读写权限,
53 |
54 | 对所有非root用户设置只读权限,防止非授权用户修改sshd服务的安全设置。
55 | ```
56 | chmod 644 /etc/ssh/sshd_config
57 | ```
58 | ### 设置TCP Wrappers
59 |
60 | 使用TCPWrappers可以阻止或允许应用服务仅对某些主机开放,给系统在增加一道安全屏障。
61 |
62 | 这部分设置共涉计到两个文件:hosts.allow和hosts.deny。
63 |
64 | 如系统仅允许IP地址为192.168.0.15和10.0.0.11的主机使用sshd服务,
65 | ```
66 | 在/etc/hosts.allow中添加
67 | sshd:192.168.0.15 10.0.0.11
68 |
69 | 在/etc/hosts.deny中添加
70 | sshd:All
71 | ```
72 | 注意:系统对上述两个文件的判断顺序是先检查hosts.allow文件再查看hosts.deny文件
73 |
74 | ### 尽量关闭一些系统不需要的启动服务
75 |
76 | ## 其他
77 |
78 | 检测近期用户登录登陆情况
79 |
80 | [shell_menu](https://github.com/BillWang139967/shell_menu/wiki)
81 |
--------------------------------------------------------------------------------
/06_denyhosts/denyhosts.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | #Denyhosts SHELL SCRIPT
3 |
4 | cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"=" $1;}' >/tmp/Denyhosts.txt
5 | DEFINE="10"
6 | for i in `cat /tmp/Denyhosts.txt`
7 | do
8 | IP=`echo $i|awk -F= '{print $1}'`
9 | NUM=`echo $i|awk -F= '{print $2}'`
10 | if [ $NUM -gt $DEFINE ]
11 | then
12 | grep $IP /etc/hosts.deny >/dev/null
13 | if [ $? -gt 0 ];
14 | then
15 | echo "sshd:$IP" >> /etc/hosts.deny
16 | fi
17 | fi
18 | done
19 |
--------------------------------------------------------------------------------
/07_audit_action/README.md:
--------------------------------------------------------------------------------
1 | ## 使用[PROMPT_COMMAND]变量 实现审计操作行为功能
2 |
3 | ### 安装
4 | ```
5 | #curl -o audit_action.sh https://raw.githubusercontent.com/BillWang139967/linux_tools/master/07_audit_action/audit_action.sh
6 | #bash audit_action.sh
7 | ```
8 | ### 提示
9 |
10 | 此日志文件如果需要删除时,在执行删除操作时,会提示没有权限,这个就是chattr对文件实现的保护功能了,如果需要删除此文件,需要执行
11 |
12 | ```
13 | chattr -a /var/log/Command_history.log
14 | chmod 777 /var/log/Command_history.log
15 | ```
16 |
17 | 然后对日志文件进行删除操作
18 |
19 | ### 相关
20 |
21 | [ssh_menu](https://github.com/BillWang139967/shell_menu)
22 |
--------------------------------------------------------------------------------
/07_audit_action/audit_action.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | FILENAME="/var/log/Command_history.log"
4 | PATHNAME="/etc/profile"
5 | FINDNAME="HISTORY_FILE"
6 | if [[ ! -f ${FILENAME} ]]
7 | then
8 | #创建行为审计日志文件
9 | touch ${FILENAME}
10 | #将日志文件的所有者改为权限低的用户NOBODY
11 | chown nobody:nobody ${FILENAME}
12 | #赋予所有用户对日志文件写的权限
13 | chmod 002 ${FILENAME}
14 | #使所有用户对日志文件只有追加权限
15 | chattr +a ${FILENAME}
16 | fi
17 |
18 | if [[ `cat ${PATHNAME} | grep ${FINDNAME} | wc -l` < 1 ]]; then
19 | cat >> ${PATHNAME} <<"EOF"
20 | export HISTORY_FILE=/var/log/Command_history.log
21 | export PROMPT_COMMAND='{ date "+%y-%m-%d %T ##### $(who am i |awk "{print \$1\" \"\$2\" \"\$5}") #### $(history 1 | { read x cmd; echo "$cmd"; })"; } >>${HISTORY_FILE}'
22 | EOF
23 | else
24 | exit 0
25 | fi
26 |
--------------------------------------------------------------------------------
/08_rm/README.md:
--------------------------------------------------------------------------------
1 | 开启linux和mac下命令行版本rm的回收站
2 |
3 | * [适用系统](#适用系统)
4 | * [使用说明](#使用说明)
5 | * [安装](#安装)
6 | * [使用](#使用)
7 | * [功能说明](#功能说明)
8 | * [彻底删除文件](#彻底删除文件)
9 |
10 | ## 适用系统
11 |
12 | > * Linux
13 | > * mac
14 |
15 | ## 使用说明
16 |
17 | ### 安装
18 |
19 | ```
20 | #curl -o rmtrash.sh https://raw.githubusercontent.com/BillWang139967/linux_tools/master/08_rm/rmtrash.sh
21 | #mv rmtrash.sh /bin/
22 | #chmod +x /bin/rmtrash.sh
23 | #/bin/rmtrash.sh
24 | #source ~/.bashrc
25 | ```
26 |
27 | 如果想对全局所有用户启用回收站,需要修改bashrc全局配置文件后即可:
28 | echo "alias rm=/bin/rmtrash.sh" >>/etc/bashrc
29 |
30 | ### 使用
31 | rm -h
32 | Usage: rm file1 [file2] [dir3] [....] delete the files or dirs,and mv them to the rmtrash recycle bin
33 | rm is alias to rmtrash.sh.
34 | options:
35 | -f mv one or more files to the rmtrash recycle bin
36 | -r mv one or more files to the rmtrash recycle bin
37 | -fr mv one or more files to the rmtrash recycle bin
38 | -rf mv one or more files to the rmtrash recycle bin
39 | -R Restore selected files to the originalpath from rmtrash recycle bin
40 | -l list the contens of rmtrash recycle bin
41 | -i show detailed log of the deleted file history
42 | -d delete one or more files by user's input file name from the trash
43 | -e empty the rmtrash recycle bin
44 | -h display this help menu
45 |
46 | ## 功能说明
47 |
48 | ### 彻底删除文件
49 | ```
50 | # rm -e 清空回收站
51 | # /bin/rm file 直接删除文件而不经过回收站
52 | ```
53 |
--------------------------------------------------------------------------------
/08_rm/rmtrash.sh:
--------------------------------------------------------------------------------
1 | #########################################################################
2 | # File Name: w.sh
3 | # Author: meetbill
4 | # mail: meetbill@163.com
5 | # Created Time: 2016-12-01 23:46:50
6 | #########################################################################
7 | #!/bin/bash
8 | ### rmtrash,rm command line recycle bin for linux and mac osx.
9 | ### rmtrash 是linux和mac下命令行版本rm的回收站,安装后对用户透明,符合正常使用rm的习惯(支持rm -fr file哦),有了他再也不怕rm时候手颤抖了。
10 |
11 | #####################################################################################
12 | # 更新说明:
13 | # 1.0.2 : 修复rm -e 清空回收站时无法清空.开头的隐藏文件
14 | #
15 | #
16 | #####################################################################################
17 |
18 | ###trash目录define
19 | realrm="/bin/rm"
20 | trash_dir=~/.rmtrash/
21 | trash_log=~/.rmtrash.log
22 | ###判断trash目录是否存在,不存在则创建
23 | if [ ! -d $trash_dir ] ;then
24 | mkdir -v $trash_dir
25 | fi
26 |
27 | ###动态修改用户shell中的alias配置
28 | os_type=`uname`
29 | shell_path=$SHELL
30 | shell_type=`echo $SHELL|awk -F/ '{print $NF}'`
31 | alias_file=~/.${shell_type}rc
32 | alias_rm=`cat $alias_file|grep ^"alias rm="`
33 | return_value=$?
34 | #echo return_value: $return_value
35 | #echo alias_rm: $alias_rm
36 | ###如果不存在rm alias,则生成
37 | if [[ $return_value -ne 0 ]] ;then
38 | echo first time to run rmtrash
39 | echo "alias rm=/bin/rmtrash.sh" >>$alias_file && source $alias_file
40 | ###如果存在rm alias,且不是指向rmtrash的,则注释掉,区分linux 和mac
41 | elif [[ "$alias_rm" != "alias rm=/bin/rmtrash.sh" ]];then
42 | echo already has alias rm,and must commit out
43 | if [[ $os_type == Darwin ]];then
44 | sed -i .bak 's/^alias\ rm=/#alias\ rm=/g' $alias_file && \
45 | echo "alias rm=/bin/rmtrash.sh" >>$alias_file && \
46 | source $alias_file
47 | elif [[ $os_type == Linux ]];then
48 | sed -i.bak 's/^alias\ rm=/#alias\ rm=/g' $alias_file && \
49 | echo "alias rm=/bin/rmtrash.sh" >>$alias_file && \
50 | source $alias_file
51 | fi
52 | fi
53 |
54 | ####function define
55 | ###usage function
56 | rm_usage () {
57 | cat <> $trash_log && \
110 | echo -e "\033[31m\033[05m $file is deleted from $file_fullpath\033[0m"
111 | #cat $trash_log
112 | fi
113 |
114 | #fi
115 | ###done
116 | }
117 |
118 | ###rm list function
119 | rm_list () {
120 | echo ----------------------------
121 | echo list trash_dir contents:
122 | ls $trash_dir
123 | }
124 |
125 |
126 | ###rm restore function
127 | rm_restore () {
128 | echo ----------------------------
129 | echo -en "请选择要恢复的文件名(多个文件中间空格分隔,取消ctl+c):"
130 | read reply
131 | for file in $reply ;do
132 | ###判断原始位置的是否有同名文件存在
133 | originalpath=`cat $trash_log|grep /$file$|awk '{print $6}'`
134 |
135 | echo $originalpath
136 |
137 | if [[ -a $originalpath ]];then
138 | echo -en "originalpath:$originalpath already exists. continue overwrite or not(y/n):"
139 | read ack
140 | if [[ $ack == y ]];then
141 | echo restore:
142 | elif [[ $ack == n ]];then
143 | echo bye && exit
144 | else
145 | echo 输入非法 && exit
146 | fi
147 | fi
148 | ###
149 | mv $trash_dir$file $originalpath && \
150 | ###linux和mac下sed的用法有细微差别,故需通过操作系统类型进行选择对应的sed格式
151 | if [[ $os_type == Darwin ]];then
152 | sed -i .bak "/\/$file$/d" $trash_log
153 | echo os_type=Darwin
154 | elif [[ $os_type == Linux ]];then
155 | sed -i.bak "/\/$file$/d" $trash_log
156 | echo os_type=Linux
157 | fi && \
158 | echo -e "\033[32m\033[05m$file restore ok to originalpath=$originalpath\033[0m"
159 | done
160 | }
161 |
162 | ### rm show delete log function
163 | rm_infolog () {
164 | echo ----------------------------
165 | echo detailed deleted file log:
166 | cat $trash_log
167 | }
168 |
169 |
170 | ###rm empty trash function
171 | rm_empty () {
172 | echo ----------------------------
173 | echo -en "empty trash,all backups in trash will be deleted, continue or not(y/n):"
174 | read ack
175 | if [[ $ack == y ]];then
176 | echo begin to empty trash:
177 | elif [[ $ack == n ]];then
178 | echo bye && exit
179 | else
180 | echo 输入非法 && exit
181 | fi
182 | if [[ -d ${trash_dir} ]]
183 | then
184 | /bin/rm -fr ${trash_dir} && \
185 | echo >$trash_log && \
186 | echo -e "\033[31m\033[05m The trash bin has been emptyed\033[0m"
187 | else
188 | echo "trash_dir is not exists"
189 | fi
190 | }
191 |
192 | ###rm delete function
193 | rm_delete () {
194 | echo ----------------------------
195 | echo -en "请选择trash中要删除的文件名(多个文件中间空格分隔,取消ctl+c):"
196 | read reply
197 | for file in $reply ;do
198 | ###if file exist then delete it from trash
199 | if [[ -a ${trash_dir}$file ]];then
200 | /bin/rm -fr ${trash_dir}$file && \
201 | ###linux和mac下sed的用法有细微差别,故需通过操作系统类型进行选择对应的sed格式
202 | if [[ $os_type == Darwin ]];then
203 | sed -i .bak "/\/$file$/d" $trash_log
204 | echo os_type=Darwin
205 | elif [[ $os_type == Linux ]];then
206 | sed -i.bak "/\/$file$/d" $trash_log
207 | echo os_type=Linux
208 | fi && \
209 | echo -e "\033[32m\033[05m$file is deleted from trash ${trash_dir}$file \033[0m"
210 | else
211 | echo $file is not exist in $trash_dir
212 | fi
213 | done
214 | }
215 |
216 | ###清空回收站中30天之前执行rm删除过的文件
217 | rm_delete_by_30_days () {
218 | rm_mv_30_days_ago_timestamp=$1
219 | ###30*24*3600=2592000
220 | #30_days_by_seconds=2592000
221 | #cat $trash_log|awk 'BEGIN{30_days_by_seconds=2592000}{if()}'
222 | awk 'END{
223 | print 时间差:$2-2592000
224 | {if ($2-2592000>100) print dayu}
225 | }
226 | ' $trash_log
227 | }
228 |
229 | ###跨分区的问题
230 |
231 | #####主程序开始
232 | ###参数个数为0,输出help
233 | if [ $# -eq 0 ] ;then rm_usage ;fi
234 | ###根据用户输入选项执行相应动作
235 | ###通过非显示的方式(加入fr选项,但在case里不做匹配操作,遇到含-fr/-rf/-f/-r时直接删除)支持很多用户的使用习惯rm -fr file,rm -rf file
236 | while getopts lRiecdhfr option ;do
237 | case "$option" in
238 | l) rm_list;;
239 | R) rm_list
240 | rm_restore;;
241 | i) rm_infolog;;
242 | h) rm_usage;;
243 | e) rm_empty;;
244 | c) rm_delete_by_30_days;;
245 | d) rm_list
246 | rm_delete;;
247 | \?)rm_usage
248 | exit 1;;
249 | esac
250 | done
251 | shift $((OPTIND-1))
252 |
253 | ###将文件名的参数依次传递给rm_mv函数
254 | while [ $# -ne 0 ];do
255 | file=$1
256 | echo file=$file
257 | rm_mv
258 | shift
259 | done
260 |
261 |
262 |
--------------------------------------------------------------------------------
/09_screen/README.md:
--------------------------------------------------------------------------------
1 | # 开启screen 状态栏
2 |
3 | * [适用系统](#适用系统)
4 | * [使用说明](#使用说明)
5 | * [安装](#安装)
6 | * [screen 使用](#screen-使用)
7 |
8 | ## 适用系统
9 |
10 | > * Linux
11 |
12 | ## 使用说明
13 |
14 | ### 安装
15 |
16 | ```
17 | #curl -o screen.sh https://raw.githubusercontent.com/BillWang139967/linux_tools/master/09_screen/screen.sh
18 | #bash screen.sh
19 | ```
20 | ### screen 使用
21 |
22 | > * C-a c 创建一个新的运行shell的窗口并切换到该窗口
23 | > * C-a C-a 切换到之前显示的窗口
24 | > * C-a n 切换到下一个窗口
25 | > * C-a p 切换到前一个窗口
26 | > * C-a d 断开所有 screen 终端,返回 screen 执行前状态,但 screen 内所有终端的任务都在执行
27 |
--------------------------------------------------------------------------------
/09_screen/screen.sh:
--------------------------------------------------------------------------------
1 | #########################################################################
2 | # File Name: screen.sh
3 | # Author: meetbill
4 | # mail: meetbill@163.com
5 | # Created Time: 2016-12-07 23:17:51
6 | #########################################################################
7 | #!/bin/bash
8 |
9 | cat > ~/.screenrc << EOF
10 | # meetbill
11 | # meetbill@163.com
12 | hardstatus on
13 | hardstatus alwayslastline
14 | hardstatus string "%{= G}%-Lw%{= .Y}%50> %n*%f %t%{= G}%+Lw%< %{= G}%-=%D %c:%s %m/%d/%Y"
15 |
16 | # 关闭screen的startup message
17 | startup_message off
18 |
19 | # 关闭闪屏
20 | vbell off
21 | autodetach on
22 | msgwait 1
23 | shell bash
24 | termcapinfo xterm|xterms|xs|rxvt|urxvt|tila ti@:te@
25 | EOF
26 |
27 |
--------------------------------------------------------------------------------
/10_rebuild_centos/rebuild_centos6.x_X64_image.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | #拷贝系统安装所需的软件包
3 | #author:王斌
4 |
5 | SYSTEM_DIR=/home/centos
6 | SYSTEM_NAME=itnihaoOS
7 | ISO_MOUNT_POIONT1=/mnt/cd1
8 | ISO_MOUNT_POIONT2=/mnt/cd2
9 |
10 | mount_ISO (){
11 | mkdir ${ISO_MOUNT_POIONT1}
12 | mkdir ${ISO_MOUNT_POIONT2}
13 | mkdir ${SYSTEM_DIR}/Packages -p
14 | mkdir /home/source
15 | echo "请确保光驱里面有ISO文件,且可以被挂载"
16 | echo "请输入Y/y继续,任意键则退出运行"
17 | read ret
18 | [ ${ret} != "Y" -a ${ret} != "y" ] && exit 1
19 | echo "echo 默认情况,挂载的是/dev/cdrom到${ISO_MOUNT_POIONT1}"
20 | ls ${ISO_MOUNT_POIONT1} |grep Packages
21 | if [ "$?" == 0 ]
22 | then
23 | echo "光驱已经挂载"
24 | else
25 | echo "正在尝试挂载本地光驱到/mnt/cd1,请稍等片刻"
26 | mount /dev/cdrom ${ISO_MOUNT_POIONT1}
27 | ls ${ISO_MOUNT_POIONT1} |grep Packages
28 | if [ "$?" != 0 ]
29 | then
30 | echo "光盘挂载不成功,请手动重新挂载,或者尝试本地ISO挂载"
31 | echo "请输入本地ISO的路径:"
32 | read ret
33 | ls ${ret}
34 | [ "$?" != 0 ] && echo "本地ISO不存在或者路径错误,退出运行" && exit 1
35 | [ "$?" == 0 ] && mount -o loop ${ret} ${ISO_MOUNT_POIONT1}
36 | fi
37 | fi
38 | }
39 |
40 |
41 | copy_ISO_file (){
42 | awk '{print $2}' install.log |sed -e '/^$/d' -e 's/^ //g'|grep -v FINISHED|grep -v ":" >/home/source/packges.list
43 | for packges in $(cat /home/source/packges.list)
44 | do
45 | cp ${ISO_MOUNT_POIONT1}/Packages/$packges* ${SYSTEM_DIR}/Packages
46 | [ $? != 0 ] && echo "copy $packges is faied!"&& cp ${ISO_MOUNT_POIONT2}/Packages/$packges* ${SYSTEM_DIR}/Packages
47 | [ $? != 0 ] && echo "$packges is not exist in ${ISO_MOUNT_POIONT2}/Packages/"
48 | done
49 | rsync -a --exclude=Packages ${ISO_MOUNT_POIONT1}/ ${SYSTEM_DIR}
50 | }
51 |
52 |
53 | rebuild_repo_xml (){
54 | yum -y install createrepo mkisofs
55 | cd ${SYSTEM_DIR}
56 | declare -x discinfo=$(head -1 .discinfo)
57 | ##########################centos6.3_X64###############################
58 | #mv ${SYSTEM_DIR}/repodata/*x86_64-comps.xml ${SYSTEM_DIR}/repodata/comps.xml
59 | #createrepo -g ${SYSTEM_DIR}/repodata/comps.xml ${SYSTEM_DIR}
60 | #createrepo -u "media://$discinfo" -g ${SYSTEM_DIR}/repodata/comps.xml ${SYSTEM_DIR}
61 | ######################################################################
62 |
63 |
64 | ##########################centos6.4_X64###############################
65 | mv ${SYSTEM_DIR}/repodata/*x86_64-comps.xml ${SYSTEM_DIR}/repodata/c6-x86_64-comps.xml
66 | createrepo -g ${SYSTEM_DIR}/repodata/c6-x86_64-comps.xml ${SYSTEM_DIR}
67 | createrepo -u "media://$discinfo" -g ${SYSTEM_DIR}/repodata/c6-x86_64-comps.xml ${SYSTEM_DIR}
68 |
69 | #mkisofs -o ${SYSTEM_NAME}.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -R -J -v -V itnihao -T ${SYSTEM_DIR}
70 | }
71 |
72 | isolinux_cfg (){
73 | cat > ${SYSTEM_DIR}/isolinux/isolinux.cfg < ${SYSTEM_DIR}/isolinux/ks.cfg <" --checklist "" [ - ] . . .
5 |
6 |
7 | VERSION=$(whiptail --title "Linux Distro version" --checklist \
8 | "What distro are you running?" 15 60 4 \
9 | "Mint" "Basic usage" ON \
10 | "Ubuntu" "Desktop usage" OFF \
11 | "Debian" "Desktop & Server" OFF \
12 | "CentOS" "Server usage" OFF 3>&1 1>&2 2>&3)
13 |
14 | exitstatus=$?
15 |
16 | if [ $exitstatus = 0 ]; then
17 | echo "The chosen distro is:" $VERSION
18 | else
19 | echo "You chose Cancel."
20 | fi
21 |
--------------------------------------------------------------------------------
/11_shell_common/shell_whiptail/input-box.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # Syntax
4 | # whiptail --title "" --inputbox ""
5 |
6 |
7 | MACHINE=$(whiptail --title "Configure profile" --inputbox "What is your Computer Name?" 10 60 Ubuntu 3>&1 1>&2 2>&3)
8 |
9 | exitstatus=$?
10 | if [ $exitstatus = 0 ]; then
11 | echo "Your Computer Name is:" $MACHINE
12 | else
13 | echo "You chose Cancel."
14 | fi
15 |
--------------------------------------------------------------------------------
/11_shell_common/shell_whiptail/menu-box.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # Syntax
4 | # whiptail --title "