企业中常用的监控命令
控制目标 | 命令 |
netstat -lntup | |
本地端口监控 | ss -lntup |
lsof | |
telnet | |
远端端口控制 | nc |
nmap | |
进程监控 | ps -ef |
ps aux | |
web监控 | curl |
wget | |
数据库 | mysql -uroot -p123 'select ping ()' |
内存 | free -m |
磁盘 | df -h |
文件内容 | md5 |
端口检查
本地端口检测
## 错误方式
[root@web01 ~]# netstat -lntup|grep '22'
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 856/sshd
tcp6 0 0 :::22 :::* LISTEN 856/sshd
## netstat 正确方式
[root@web01 ~]# netstat -lntup|grep [s]shd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 856/sshd
tcp6 0 0 :::22 :::* LISTEN 856/sshd
[root@web01 ~]# netstat -lntup|grep -w '22'
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 856/sshd
tcp6 0 0 :::22 :::* LISTEN 856/sshd
[root@web01 ~]# netstat -lntup|grep -w '22' &>/dev/null
[root@web01 ~]# echo $?
0
[root@web01 ~]# netstat -lntup|grep -w '10050' &>/dev/null
[root@web01 ~]# echo $?
1
[root@web01 ~]# netstat -lntup|grep -w '22'|wc -l
2
[root@web01 ~]# netstat -lntup|grep -w '10050'|wc -l
0
####以上是脚本演变过程
--------------------------------------------------
####以下是脚本
#!/bin/bash
# File Name: __net.sh__
# Version: __v1.1__
# Author: __yjt__
# Mail: __1781811351@qq.com__
IP=$1
port_count=`echo '' |telnet $IP 22 2>/dev/null |grep 'Connected'|wc -l`
if [ $port_count -eq 0 ];then
echo '端口不存活'
else
echo '端口存活'
fi
------------------------------------------------
## ss
[root@web01 ~]# ss -lntup|grep 22
tcp LISTEN 0 128 *:22 *:* users:(("sshd",pid=856,fd=3))
tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=856,fd=4))
## lsof
[root@web01 ~]# lsof -i:22
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 856 root 3u IPv4 20083 0t0 TCP *:ssh (LISTEN)
sshd 856 root 4u IPv6 20092 0t0 TCP *:ssh (LISTEN)
sshd 1104 root 3u IPv4 21008 0t0 TCP web01:ssh->10.0.0.1:64772 (ESTABLISHED)
### lsof需要下载
使用脚本判断远程端口是否存活
## 使用telnet端口扫描
#!/bin/bash
# File Name: __ps-ef.sh__
# Version: __v1.1__
# Author: __yjt__
# Mail: __1781811351@qq.com__
. /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
## nc 网络中的瑞士军刀
[root@web01 ~]# echo ''|nc 172.16.1.8 80
Ncat: Connection refused.
[root@web01 ~]# echo $?
1
[root@web01 ~]# echo ''|nc 172.16.1.8 22
SSH-2.0-OpenSSH_7.4
Protocol mismatch.
[root@web01 ~]# echo $?
0
# nc选项
-l:开启一个指定的端口
-k:保持端口持续连接
-u:指定nc使用udp协议(默认tcp)
-s:指定发送数据的源IP地址,适用于多网卡机器
-w:设置超时时间
-z:扫描时不发送任何数据
## nmap
# 单个IP扫描
[root@m01 ~]# nmap 172.16.1.7
# 单个IP的单个端口扫描
[root@m01 ~]# nmap -p 22 172.16.1.7
# 单个IP的范围端口扫描
[root@m01 ~]# nmap -p 1-65535 172.16.1.7
# 多个IP的范围端口扫描
[root@m01 ~]# nmap -p 1-65535 172.16.1.7 baidu.com
进程判断
[root@web01 ~]# ps -ef|grep [s]sh
root 856 1 0 08:17 ? 00:00:00 /usr/sbin/sshd -D
root 10464 856 0 15:08 ? 00:00:00 sshd: root@pts/0
[root@web01 ~]# ps -ef|grep [s]shd| wc -l
2
[root@web01 ~]# ps -ef|grep [s]shd111| wc -l
0
--------------------------------------
## 远程进程检测脚本
#!/bin/bash
# File Name: __gerp.sh__
# Version: __v1.1__
# Author: __yjt__
# Mail: __1781811351@qq.com__
IP=$1
proc_count=`ssh $IP 'ps -ef|grep [n]ginx|wc -l'`
if [[ $proc_count != 0 ]];then
echo 'nginx不存活'
else
echo 'nginx存活'
fi
网站检测
curl选项
-I:获取主机响应头部信息
-s:取消默认输出
-o:保存下载页面内容
-w:获取状态码
-u:身份认证 -u 用户名:密码
-H:添加请求头部信息
-v:显示详细信息
-L:跟随跳转
-X:指定请求方式
-A:修改用户的客户端
[root@web01 ~]# curl -s -w "%{http_code}" -o /dev/null baidu.com
200
## -u的写法:curl -uzls:zls -s -w "%{http_code}" -o /dev/null blog.zls.com 前面的是账号,后面是密码
# 第二种写法:适用性高,基本上的都能用 url -s -w "%{http_code}" -o /dev/null http://zls:zls@blog.zls.com
[root@web01 ~]# curl -v http://www.360buy.com -L
京东的网站,会跳转最后到https。jd.com
wget选项
-O:保存下载页面内容
-r:递归下载
--debug:显示访问的详细过程 类似 curl -v
-q:静默输出 类似 curl -s
--spider:只查看不下载
文件检测
[root@web01 opt]# touch 1.txt
[root@web01 opt]# md5sum 1.txt
d41d8cd98f00b204e9800998ecf8427e 1.txt
[root@web01 opt]# md5sum 1.txt > /opt/2.txt
[root@web01 opt]# md5sum -c /opt/2.txt
1.txt: OK
[root@web01 opt]# echo 111 > 1.txt
[root@web01 opt]# md5sum -c /opt/2.txt
1.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
## 把1.txt文件重定向到2.txt,然后如果再1.txt上添加东西,就会文件不一致,会报错