netstat 指令将所有的网络端口监听情况进行罗列
语法 netstat -tuln
几个常见的服务端口

例 通过grep 查看端口来获得上面的服务是否开启,并给予提示
1 #!/bin/bash
2 #Program:
3 # Using netstat and grep to detect WWW,SSH,FTP and Mail service
4 #History:
5 #2019-7-29 lsq First release
6 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
7 export PATH
8
9 #tell some action
10 echo "Now, I will detect your linux server's services !"
11 echo -e "The www,ftp,ssh,and mail(smtp) will be detect! \n"
12
13 #start some detect work ,to echo some information
14 testfile=/dev/shm/netstat_checking.txt
15 netstat -tuln > ${testfile}
16 testing=$(grep ":80 " ${testfile})
17 if [ "${testing}" != "" ]; then
18 echo "www is running in your system."
19 fi
20
21 testing=$(grep ":22" ${testfile})
22 if [ "${testing}" != "" ]; then
23 echo "SSH is running in your system"
24 fi
25
26 testing=$(grep ":21" ${testfile})
27 if [ "${testing}" != "" ]; then
28 echo "FTP is runing in your system"
29 fi
30
31 testing=$(grep ":25" ${testfile})
32 if [ "${testing}" != "" ]; then
33 echo "Mail is runing in your system"
34 fi
上面的例子有几个坑,需要注意一下
第一个 还是空格问题。
testing=$(grep ":22" ${testfile})
=号前后没有空格,切记。
第二个 if [ ] 括号中前后需要有两个空格
第三个 if中
"${testing}" != ""
!=前后都有空格
第四个 解析
上面例子的意思,用netstat -tuln命令将机器所有的服务端口信息保存到/dev/shm/netstat_checking.txt文件中
然后用grep针对端口进行查找,然后在分类输出。原理比较简单,就是那些坑,要注意,否则容易报错。
浙公网安备 33010602011771号