netstat 指令将所有的网络端口监听情况进行罗列
语法 netstat -tuln
几个常见的服务端口
例 通过grep 查看端口来获得上面的服务是否开启,并给予提示
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 | 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 |
上面的例子有几个坑,需要注意一下
第一个 还是空格问题。
1 | testing=$( grep ":22" ${testfile}) |
=号前后没有空格,切记。
第二个 if [ ] 括号中前后需要有两个空格
第三个 if中
1 | "${testing}" != "" |
!=前后都有空格
第四个 解析
上面例子的意思,用netstat -tuln命令将机器所有的服务端口信息保存到/dev/shm/netstat_checking.txt文件中
然后用grep针对端口进行查找,然后在分类输出。原理比较简单,就是那些坑,要注意,否则容易报错。
分类:
Linux
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix