上一个脚本其实并不难,只要把你做过的操作整理一遍,把该有的命令写到脚本里就可以了。为什么大家都不去动手?其实厉害的大牛和普通人的区别就在于此,大牛一开始也是小白,但是他们努力,有目标,会动手去写。不要找借口说不会,那我问你,lamp你搭建成功了吗?如果搭建成功了就可以写出那个脚本来!
今天的shell练习题:写一个脚本判断你的Linux服务器里是否开启web服务?(监听80端口)如果开启了,请判断出跑的是什么服务,是httpd呢还是nginx又或者是其他的什么? (本来还想再加一个需求,我看还是算了,不然你们又不动手了。)
今天的shell练习题:写一个脚本判断你的Linux服务器里是否开启web服务?(监听80端口)如果开启了,请判断出跑的是什么服务,是httpd呢还是nginx又或者是其他的什么? (本来还想再加一个需求,我看还是算了,不然你们又不动手了。)
0
上回的没写 这回占个沙发吧 {:5_121:}
# cat 1014.sh
#!/bin/bash
# Desc : this script is to check web app services
# Writen by Jeffery.Su
# Date: Oct,14 2014
PORT=80
SERVICE=$(netstat -ntlp | grep :$PORT| awk -F"/" '{print $2}'|uniq)
if [ -z $SERVICE ];then
echo "System web app is not runing "
else
echo "System current web app is $SERVICE"
fi
0
思路:netstat -nlp查看80端口是否开启,如开启,则web服务已启动,然后通过该pid查看(ps aux)到底是哪个进程在运行;如未开启,则web服务未启动。
- #!/bin/bash
- ## This script is for judge if web service is on,and what service provides.
- ## Written by Louis at 2014/10/14 17:45
- if netstat -nlp|grep -v 'grep'|grep -q ':80'; then
- web_pid=`netstat -nlp|grep ':80'|awk '{print $NF}'|awk -F/ '{print $1}'`
- web_ser=`ps aux|grep -v 'grep'|grep $web_pid|awk '{print $11}'`
- echo "web service is on,it's main progress is '$web_ser'"
- else
- echo "web service is off."
- fi
0
- #!/bin/bash
- # 2014-10-14
- # chenpei
- # Whether to start the web service and service type
- whether=`netstat -lnp | grep 80`
- if [ -z "$whether" ]; then
- echo "not start service.";
- exit;
- fi
- web_server=`echo $whether | awk -F'/' '{print $2}'`
- case $web_server in
- httpd )
- echo "apache server."
- ;;
- nginx )
- echo "nginx server."
- ;;
- * )
- echo "other server."
- ;;
- esac
0
本帖最后由 程城 于 2014-10-14 22:48 编辑
#!/bin/bash
#find the service which listen the port of 80!
port=80
port_ifup=`netstat -lnp |grep -c $port`
if [ $port_ifup -ge 1 ];then
echo "port of 80 is up!"
services=`netstat -lnp |grep $port |awk '{print $7}'|sed 's#^.*/##g'|uniq`
echo "$services web services is running the port of 80 !"
else
echo "Have no web services running to the port of 80 !"
fi
0
- #!/bin/bash
- #Check 80 on/off
- #yum -y install lsof >/dev/null 2>&1
- lsof -i:80 >/dev/null 2>&1
- [ `echo $?` -eq 0 ] && echo "web server is running!"
- web=`lsof -i:80 |awk '{print $1}' |grep -v 'COMMAND' |sort |uniq`
- [ ! -z $web ] && echo "The web server is $web"
0
#!/bin/bash
#20141014练习题
for line in `netstat -lnp | grep :80 | awk '{printf $7}' | awk -F/ '{printf $NF}'`
do
if [ -n $line ]
then
echo "WebServer has been started!"
echo "ServerName:$line"
else
echo "Don't start WebServer!"
fi
done
#20141014练习题
for line in `netstat -lnp | grep :80 | awk '{printf $7}' | awk -F/ '{printf $NF}'`
do
if [ -n $line ]
then
echo "WebServer has been started!"
echo "ServerName:$line"
else
echo "Don't start WebServer!"
fi
done
0
本帖最后由 zyfeifie 于 2014-10-16 11:08 编辑
- #/bin/bash
- name=`netstat -lntp | awk '$4~/:80$/ {print $NF}' | awk -F '/' '{print $2}'
- if [ ! -z $name ];then
- echo "the web service is running."
- echo "the webservice is $name."
- else
- echo "the webservice is not runing"
- fi
0
本帖最后由 陌懒 于 2014-10-16 18:03 编辑
- #!/bin/bash
- test1=$(netstat -tuln|grep ":80")
- test2=$(netstat -tulnp|grep "httpd")
- if [ "$test1" != "" ];then
- if [ "$test2" != "" ];then
- echo "The system is running http."
- else
- echo "The system is running nginx."
- fi
- else
- echo "Web is not running in your system!"
- fi
0
- #!/bin/bash
- service=`netstat -lnp |grep 80|awk -F '/' '{print $2}'`
- if [ "$service" == "" ]
- then
- echo "not web server runing!"
- elif [ "$service" == "httpd" ]
- then
- echo "httpd server runing!"
- elif [ "$service" == "nginx" ]
- then
- echo "nginx server runing!"
- else
- echo "$service server runing!"
- fi
0
本帖最后由 soar 于 2014-10-17 12:01 编辑
#!/bin/bash
web=`netstat -lnpt | grep 80`
if [ "$web" != "" ]
then
echo "the web server is running"
else
echo "the web server is not running"
fi
#判断开启的是httpd 还是nginx
httpd=`/usr/bin/pgrep httpd`
nginx=`/usr/bin/pgrep nginx`
if [ "$httpd" != "" ]
then
echo "the running web server is httpd"
elif [ "$nginx" != "" ]
then
echo "the runing web server is nginx"
else
echo " the web server is not httpd or nginx"
fi
#!/bin/bash
web=`netstat -lnpt | grep 80`
if [ "$web" != "" ]
then
echo "the web server is running"
else
echo "the web server is not running"
fi
#判断开启的是httpd 还是nginx
httpd=`/usr/bin/pgrep httpd`
nginx=`/usr/bin/pgrep nginx`
if [ "$httpd" != "" ]
then
echo "the running web server is httpd"
elif [ "$nginx" != "" ]
then
echo "the runing web server is nginx"
else
echo " the web server is not httpd or nginx"
fi
0
#!/bin/bash
n=`netstat -lnp|grep -w 80|awk -F '/' '{print $NF}'`
if [ $n == "" ]; then
echo "Port 80 is not Listenning."
else
echo "Port 80 is Listenning. The server is: $n"
fi
n=`netstat -lnp|grep -w 80|awk -F '/' '{print $NF}'`
if [ $n == "" ]; then
echo "Port 80 is not Listenning."
else
echo "Port 80 is Listenning. The server is: $n"
fi
0
本帖最后由 sss 于 2015-6-1 14:12 编辑
- #!/bin/bash
- #Check web service type
- #2015/05/13
- web_ser=`netstat -nltp | grep '80' | awk '{print $NF}' | awk -F/ '{print $2}'`
- if [ -z "$web_ser" ]
- then
- echo " web services is not runing "
- else
- echo " $web_ser services is running "
- fi
0
#!/bin/bash
web=`netstat -lnp | grep :80`
ser=`netstat -lnp | grep :80|awk -F "/" '{print $2}'`
if [ -z "$web" ];then
echo "80端口没被运行"
else
echo "现在正在运行的服务是$ser"
fi
web=`netstat -lnp | grep :80`
ser=`netstat -lnp | grep :80|awk -F "/" '{print $2}'`
if [ -z "$web" ];then
echo "80端口没被运行"
else
echo "现在正在运行的服务是$ser"
fi
0
#!/bin/bash
SERV=`netstat -lnp|grep -E ':80\>'`
SERV1=`netstat -lnp|grep -E ':80\>'|wc -l`
if [ $SERV1 -eq 0 ];then
echo "80 port is not exits"
exit 1
fi
SER=`echo "$SERV" |awk -F'/' '{print $2}'|awk -F' ' '{print $1}'`
case $SER in
httpd)
echo "80 port is httpd service"
;;
nginx)
echo "80 port is nginx service"
;;
*)
echo "80 port service is unknown"
;;
esac
SERV=`netstat -lnp|grep -E ':80\>'`
SERV1=`netstat -lnp|grep -E ':80\>'|wc -l`
if [ $SERV1 -eq 0 ];then
echo "80 port is not exits"
exit 1
fi
SER=`echo "$SERV" |awk -F'/' '{print $2}'|awk -F' ' '{print $1}'`
case $SER in
httpd)
echo "80 port is httpd service"
;;
nginx)
echo "80 port is nginx service"
;;
*)
echo "80 port service is unknown"
;;
esac
0
#!/bin/bash
n=`netstat -lnp|grep ':80'`
if [ -n "$n" ];then
f=`echo $n|awk '{print $NF}'|awk -F '/' '{print $2}'`
echo "$f is running."
else
echo "没有开启web服务"
fi
n=`netstat -lnp|grep ':80'`
if [ -n "$n" ];then
f=`echo $n|awk '{print $NF}'|awk -F '/' '{print $2}'`
echo "$f is running."
else
echo "没有开启web服务"
fi
0
- #!/bin/bash
- read -p "please input a port to display program:" port
- result=`netstat -lntp|awk '{print $4}'|grep -w $port`
- if [ -z $result ]; then
- echo "Not listening port!"
- exit 0
- fi
- program=`netstat -lntp|awk 'NR>2 {print $4,$7}'|grep -w $port|cut -d '/' -f2`
- echo "The port $port is running $program!"
0
#!/bin/bash
#write by 2016-2-2
check_webtype(){
if [ $1 == "nginx" ]
then
echo "nginx is running "
elif [ $1 == "httpd" ]
then
echo "httpd is running "
fi
}
n=$(netstat -lnp|grep -E ":80"|grep -v grep)
if [ ! -z "$n" ]
then
nn=$(echo $n|awk '{print $7}'|awk -F "/" '{print $2}')
check_webtype $nn
else
echo "no web service is running "
fi
#write by 2016-2-2
check_webtype(){
if [ $1 == "nginx" ]
then
echo "nginx is running "
elif [ $1 == "httpd" ]
then
echo "httpd is running "
fi
}
n=$(netstat -lnp|grep -E ":80"|grep -v grep)
if [ ! -z "$n" ]
then
nn=$(echo $n|awk '{print $7}'|awk -F "/" '{print $2}')
check_webtype $nn
else
echo "no web service is running "
fi
0
#! /bin/bash
web_status=`netstat -lnp |grep ':80' |awk '{print $6}'`
web_app=`netstat -lnp |grep ':80' |awk '{print $7}' |sed 's#^[0-9].*/##'`
if [ $web_status==LISTEN ]
then
echo "port 80 is listening, and web is running by $web_app"
else
echo "start web service"
fi
web_status=`netstat -lnp |grep ':80' |awk '{print $6}'`
web_app=`netstat -lnp |grep ':80' |awk '{print $7}' |sed 's#^[0-9].*/##'`
if [ $web_status==LISTEN ]
then
echo "port 80 is listening, and web is running by $web_app"
else
echo "start web service"
fi
0
#!/bin/bash
a=`netstat -lnp |grep ':80'`
if [ -n "$a" ]
then
b=`netstat -lnp |grep ':80' |awk '{print $NF}' |awk -F '/' '{print $2}'`
echo "your web server is $b"
fi
a=`netstat -lnp |grep ':80'`
if [ -n "$a" ]
then
b=`netstat -lnp |grep ':80' |awk '{print $NF}' |awk -F '/' '{print $2}'`
echo "your web server is $b"
fi
编辑回复