[toc]

书写脚本常用监控命令

image-20230307085034190

本地检查端口是否开启

查看所有端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:25672 0.0.0.0:* LISTEN 37809/beam.smp
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 544/rpcbind
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 38249/nginx: master
tcp 0 0 0.0.0.0:4369 0.0.0.0:* LISTEN 4122/epmd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 770/sshd
tcp 0 0 0.0.0.0:15672 0.0.0.0:* LISTEN 37809/beam.smp
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 37783/postmaster
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 928/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 38249/nginx: master
tcp6 0 0 :::5672 :::* LISTEN 37809/beam.smp
tcp6 0 0 :::111 :::* LISTEN 544/rpcbind
tcp6 0 0 :::80 :::* LISTEN 38249/nginx: master
tcp6 0 0 :::4369 :::* LISTEN 4122/epmd
tcp6 0 0 :::22 :::* LISTEN 770/sshd
tcp6 0 0 :::5432 :::* LISTEN 37783/postmaster
tcp6 0 0 ::1:25 :::* LISTEN 928/master
tcp6 0 0 :::443 :::* LISTEN 38249/nginx: master
udp 0 0 0.0.0.0:698 0.0.0.0:* 544/rpcbind
udp 0 0 0.0.0.0:111 0.0.0.0:* 544/rpcbind
udp6 0 0 :::698 :::* 544/rpcbind
udp6 0 0 :::111 :::*

查看指定端口

1
2
3
4
5
# netstat -lntup|grep 22
tcp 0 0 0.0.0.0:4369 0.0.0.0:* LISTEN 4122/epmd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 770/sshd
tcp6 0 0 :::4369 :::* LISTEN 4122/epmd
tcp6 0 0 :::22 :::* LISTEN 770/sshd

精确查找端口

1
2
3
4
5
6
7
# netstat -lntup|grep -w '22'
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 770/sshd
tcp6 0 0 :::22 :::* LISTEN 770/sshd

# ss -lntup|grep -w 22
tcp LISTEN 0 128 *:22 *:* users:(("sshd",pid=770,fd=3))
tcp LISTEN 0 128 :::22 :::* users:(("sshd",pid=770,fd=4))

转化成数字

1
2
3
4
5
6
7
8
9
10
# netstat -lntup|grep -w '22'|wc -l
2

# 推荐使用netstat这个命令,后续我们需要监控TCP的11种状态,也用该命令
lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 770 root 3u IPv4 19637 0t0 TCP *:ssh (LISTEN)
sshd 770 root 4u IPv6 19646 0t0 TCP *:ssh (LISTEN)
sshd 25088 root 3u IPv4 491299 0t0 TCP zabbix01:ssh->gateway:61954 (ESTABLISHED)
sshd 80641 root 3u IPv4 291875 0t0 TCP zabbix01:ssh->gateway:54130 (ESTABLISHED)

检测脚本

1
2
3
4
5
6
7
8
9
10
# 编辑脚本
#!/bin/bash
IP=$1
NET=$2
port_count=`echo '' | telnet ${IP} ${NET} 2>/dev/null | grep 'Connected' | wc -l`
if [ $port_count -eq 0 ];then
echo '端口不存活'
else
echo '端口存活'
fi

远端检查端口是否开启(端口扫描)

1.telnet

1
2
3
4
5
6
7
8
9
10
11
12
# 检查端口
telnet 127.0.0.1 22
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
## 这是一个交互式的,需要我们手动退出,所以适合在命令行使用,不适合脚本中使用

# 免交互检查。
echo | telnet 127.0.0.1 22
echo | telnet 127.0.0.1 22 2>/dev/null|grep 'Connected'|wc -l
## 太麻烦,不推荐

2.nc(网络中的瑞士军刀,功能很牛)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 检查端口
echo |nc 127.0.0.1 22
SSH-2.0-OpenSSH_7.4
Protocol mismatch.

# 检查返回值
echo $?
0

# 可以跟端口建立连接,传输数据
-l:创建端口
nc -l 8888

-k:保持连接
nc -lk 8888

# 选项
-l:创建端口
-k:保持连接
-u:指定nc使用udp协议(默认tcp)
-s:指定发送数据的源IP地址,适用于多网卡机器
-w:设置超时时间
-z:扫描时不发送任何数据

3.nmap(也是一个很牛的网络扫描工具,一个命令能写一本书)

1
2
3
4
5
6
7
8
9
10
11
# 扫描IP
nmap 10.0.0.61

# 扫描一个主机的一个端口
nmap -p 22 10.0.0.61

# 扫描一个主机的多个端口
nmap -p 22-1024 10.0.0.61

# 扫描多个主机的多个端口
nmap -p 22-1024 10.0.0.61 baidu.com

检测脚本

1
2
3
4
5
6
7
8
9
10
11
# 编辑脚本1
#!/bin/bash
. /etc/init.d/functions
IP=$1
for port in `seq 65535`;do
port_count=`echo '' | telnet $IP $port 2>/dev/null | grep 'Connected' | wc -l`

if [ $port_count -ne 0 ];then
action "$port 端口" /bin/true
fi
done

服务检查 进程判断

检测脚本

1
2
3
4
5
6
7
8
# 编辑脚本
#!/bin/bash
proc_mount=`ps -ef| grep [n]ginx | wc -l`
if [ $proc_mount -eq 0 ];then
echo '服务不存活'
else
echo '服务存活'
fi

网站监测

curl命令选项

1
2
3
4
5
6
7
8
9
10
-I:           		# 只显示响应头信息
-H: # 修改请求头信息
-v: # 显示详细的请求过程
-L --location: # 跟随跳转
-s: # 不显示头部的统计信息
-X: # 指定请求方式 GET
-w: # 取出指定内容,例如:%{http_code}取出状态码
-o: # 指定输出位置
-A: # 指定User-Agent
-u: # 指定用户名密码

curl使用解析

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 下载网页"html"文件
curl www.baidu.com

# 只显示响应头部信息
curl -I www.baidu.com

HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Wed, 01 Sep 2021 01:24:34 GMT
Etag: "575e1f60-115"
Last-Modified: Mon, 13 Jun 2016 02:50:08 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

# 检查访问状态码
curl -s -w "%{http_code}" -o /dev/null https://blog.driverzeng.com
200
curl -s -w "%{http_code}\n" -o /dev/null https://blog.driverzeng.com
200

# 追踪跳转
curl www.360buy.com
curl -v www.360buy.com
curl -v www.360buy.com -L

# -s取消头部信息,一般配合管道符使用
curl -I https://blog.driverzeng.com|awk 'NR==1{print $2}'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
200

curl -I -s https://blog.driverzeng.com|awk 'NR==1{print $2}'
200

# 指定用户名密码
## 安装nginx和httpd-tools
yum install -y nginx httpd-tools
## 创建账户密码
htpasswd -b -c /etc/nginx/auth_conf zls zls
## 编辑配置文件
vim /etc/nginx/conf.d/zls.conf
server {
listen 80;
server_name _;
access_log off;

location /nginx_status {
stub_status;
auth_basic "Auth access Blog Input your Passwd!";
auth_basic_user_file auth_conf;
}
}
## 直接访问会报错401没有认证
curl 10.0.0.61/nginx_status
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
## 加上用户名密码就可以了
curl -uzls:zls 10.0.0.61/nginx_status
Active connections: 1
server accepts handled requests
5 5 3
Reading: 0 Writing: 1 Waiting: 0

## 建议这么访问
curl http://zls:zls@10.0.0.61/nginx_status
Active connections: 1
server accepts handled requests
6 6 4
Reading: 0 Writing: 1 Waiting: 0

## 访问的时候,可以修改我们自己的User-Agent,偷偷的,不让别人发现
curl -A zls_chrome https://blog.driverzeng.com

wget命令

wget选项

1
2
3
4
5
-O:      	# 指定输出位置
-r: # 递归下载
--debug: # 类似于curl的-v显示过程
-q: # 静默输出
--spider: # 不下载,就访问(爬虫模式)

wget使用解析

1
2
3
4
5
6
7
8
9
10
11
12
# 递归下载
wget -r https://nginx.org/en/docs/

# 只访问不下载
## 示例一
wget -q --spider www.baidu.com
echo $?
0
## 示例二
wget -q --spider www.baidu.com/asdasda
echo $?
8

文件检测

md5使用解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 获取文件md5值
md5sum /etc/passwd
38b06c70e862085e22f23838dc873ade /etc/passwd

# 将md5值保存到文件中
md5sum /etc/passwd > passwd.md5

# 检测md5
md5sum -c passwd.md5
/etc/passwd: OK

# 如果内容有改动则md5会发生变化
useradd test_md5
md5sum -c passwd.md5

/etc/passwd: FAILED
md5sum: WARNING: 1 computed checksum did NOT match

实战一:监控系统内存,不足30%则发送邮件通知运维人员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 服务端配置邮件功能
yum install mailx -y
vim /etc/mail.rc
set from=253097001@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=253097001@qq.com
set smtp-auth-password=#客户端授权码
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/

# 内存检测
free -m
total used free shared buff/cache available
Mem: 3934 189 2270 11 1473 3297
Swap: 1023 0 1023

1
2
3
4
5
6
7
8
9
10
11
12
# 编辑脚本
#!/bin/bash
available=`free -m | awk 'NR==2 {print $7}'`
total=`free -m | awk 'NR==2 {print $2}'`
total_warning=`awk -v x=$total 'BEGIN{print x*0.3}'`

if [ $available -eq $total_warning ];then
echo 1
else
echo 2
fi