netstat 指令

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针对端口进行查找,然后在分类输出。原理比较简单,就是那些坑,要注意,否则容易报错。
 

转载于:https://www.cnblogs.com/Lonelychampion/p/11263596.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值