用bash脚本文件检测主机上的端口是否已经开启

# !/bin/bash
# program: Using to study the [if ... then ... fi] program
# Written by Edward
# Date:2012/08/06
# content:I will using this program to show your services

# 1. print the program's work in your screen.

echo "Now,the services of your Linux system will be detect!"
echo "The ww,ftp,ssh,and sendmail + pop3 will de detected!"
echo " "

# 2. www

www='netstat -an|grep LISTEN|grep :80'
if [ "$www" != "" ];then
    echo "WWW is running."
else
    echo "WWW is NOT running."
fi

# 3.ftp

ftp='netstat -an|grep LISTEN|grep :21'
if [ "$ftp" != "" ];then
    echo "FTP is running."
else
    echo "FTP is NOT running."
fi

# 4.ssh

ssh='netstat -an|grep LISTEN|grep :22'
if [ "$ssh" != "" ];then
    echo "SSH is running."
else
    echo "SSH is NOT running."
fi

# 5. sendmail + pop3

smtp='netstat -an|grep LISTEN|grep :25'
pop3='netstat -an|grep LISTEN|grep :110'
if [ "$smtp" != "" ] && [ "$pop3" != "" ];then
    echo "sendmail is OK!"
elif [ "$smtp" != "" ] && [ "$pop3" = "" ];then
    echo "sendmail have problem of your pop3."
elif [ "$smtp" = "" ] && [ "$pop3" != "" ];then
    echo "sendmail have some problem of your smtp."
else
    echo "sendmail is NOT running."
fi

在本人电脑上运行的结果:

 

几点体会:

1. 仅仅用命令就可以检测主机的网络端口情况,挺轻松的。

2. 复习了一下网络命令‘netstat -an',用于检查网络状态。

3. shell编程虽然可以说是一种脚本式语言,但是在学习过C和C++以后,脚本式语言学习起来确实会轻松不少。

4. 对操作系统知识的掌握有助于理解shell编程的内涵。

5. shell终端下的命令其实是提供给用户以使用系统服务的接口。

 

非原创,来自《鸟哥的linux私房菜》第12章《学习使用shell scripts》

 

posted on 2012-08-06 06:38  edward1992  阅读(769)  评论(0)    收藏  举报

导航