5-29shell编程实战入门

1、服务器本地端口监控:

过滤端口然后转成行数。

netstat -lntup|grep nginx|wc -l(推荐)

ss -lntup|grep nginx|wc -l

lsof -i :80|wc -l

2、远端端口监控:

看返回的行数,为1正常,否则不正常。

nmap 127.0.0.1 -p 80|grep open|wc -l(推荐)

echo -e "\n"|telnet 127.0.0.1 80 2>/dev/null|grep Connected|wc -l

看返回值,为0正常。

[root@web01 ~]# nc -z 127.0.0.1 80

[root@web01 ~]# echo $?

0

nc的控制参数不少,常用的几个参数如下所列:

1) -l

用于指定nc将处于侦听模式。指定该参数,则意味着nc被当作server,侦听并接受连接,而非向其它地址发起连接。

2) -p

暂未用到(老版本的nc可能需要在端口号前加-p参数,下面测试环境是centos6.6,nc版本是nc-1.84,未用到-p参数)

3) -s

指定发送数据的源IP地址,适用于多网卡机

4) -u

指定nc使用UDP协议,默认为TCP

5) -v

输出交互或出错信息,新手调试时尤为有用

6)-w

超时秒数,后面跟数字

7)-z

表示zero,表示扫描时不发送任何数据

3、客户端模拟监控:看返回值或者看返回字符串

wget -q 127.0.0.1 &>/dev/null (推荐)

echo $?

curl 127.0.0.1 &>/dev/null

echo $?

7

看返回字符串是否为www7

[root@web01 /etc/nginx/conf.d]# curl -s 127.0.0.1 2>/dev/null(推荐)

www7

环境准备:

[root@oldboy ~]# touch /etc/rsyncd.conf

[root@oldboy ~]#

[root@oldboy ~]# rsync --daemon

[root@oldboy ~]# lsof -i :873

COMMAND  PID USER  FD  TYPE DEVICE SIZE/OFF NODE NAME

rsync  1690 root    4u  IPv4  14658      0t0  TCP *:rsync (LISTEN)

rsync  1690 root    5u  IPv6  14659      0t0  TCP *:rsync (LISTEN)

[root@oldboy ~]# pkill rsync

[root@oldboy ~]# lsof -i :873

启动脚本:

3、写一个rsync/sersync/nginx的启动脚本。

/etc/init.d/rsync {start|stop|restart}

C6:用chkconfig实现开机自启动管理。

C7:用systemctl实现开机自启动管理。

1)启动:rsync --daemon

2)停止:pkill rsync

  kill `cat /var/run/rsyncd.pid`


3)监控手段:netstat -lntup|grep rsync|wc -l

/etc/init.d/rsyncd {start|stop|restart}

[root@oldboy init.d]# cat rsyncd

#!/bin/bash

if [ "$1" = "start"  ]

then

  if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]

  then

      :

  else

      rsync --daemon

  fi

elif [ "$1" = "stop"  ]

then

    if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]

    then

        kill `cat /var/run/rsyncd.pid`

    else

        echo "Failed to stop rsync Unit rsync not loaded."

        exit 1

    fi

elif [ "$1" = "restart"  ]

then

    if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]

    then

        kill `cat /var/run/rsyncd.pid`

    fi

  sleep 2

  if [ -f /var/run/rsyncd.pid -a -s /var/run/rsyncd.pid ]

  then

      :

  else

      rsync --daemon

  fi

else

    echo "Usage;$0 {start|stop|restart}"

fi

C7:systemctl

c6:管理开机自启动服务,用的chkconfig

[root@oldboy init.d]# cat /etc/redhat-release

CentOS release 6.9 (Final)

[root@oldboy init.d]#

[root@oldboy init.d]#

[root@oldboy init.d]#

[root@oldboy init.d]# chkconfig --list

abrt-ccpp      0:off 1:off 2:off 3:on 4:off 5:on 6:off

abrtd          0:off 1:off 2:off 3:on 4:off 5:on 6:off

acpid          0:off 1:off 2:on 3:on 4:on 5:on 6:off

atd            0:off 1:off 2:off 3:on 4:on 5:on 6:off

auditd        0:off 1:off 2:on 3:on 4:on 5:on 6:off

blk-availability 0:off 1:on 2:on 3:on 4:on 5:on 6:off

cpuspeed      0:off 1:on 2:on 3:on 4:on 5:on 6:off

crond          0:off 1:off 2:on 3:on 4:on 5:on 6:off

haldaemon      0:off 1:off 2:off 3:on 4:on 5:on 6:off

htcacheclean  0:off 1:off 2:off 3:off 4:off 5:off 6:off

httpd          0:off 1:off 2:off 3:off 4:off 5:off 6:off

ip6tables      0:off 1:off 2:on 3:on 4:on 5:on 6:off

iptables      0:off 1:off 2:on 3:on 4:on 5:on 6:off

irqbalance    0:off 1:off 2:off 3:on 4:on 5:on 6:off

kdump          0:off 1:off 2:off 3:on 4:on 5:on 6:off

lvm2-monitor  0:off 1:on 2:on 3:on 4:on 5:on 6:off

mdmonitor      0:off 1:off 2:on 3:on 4:on 5:on 6:off

messagebus    0:off 1:off 2:on 3:on 4:on 5:on 6:off

netconsole    0:off 1:off 2:off 3:off 4:off 5:off 6:off

netfs          0:off 1:off 2:off 3:on 4:on 5:on 6:off

network        0:off 1:off 2:on 3:on 4:on 5:on 6:off

nfs-rdma      0:off 1:off 2:off 3:off 4:off 5:off 6:off

ntpd          0:off 1:off 2:off 3:off 4:off 5:off 6:off

ntpdate        0:off 1:off 2:off 3:off 4:off 5:off 6:off

postfix        0:off 1:off 2:on 3:on 4:on 5:on 6:off

psacct        0:off 1:off 2:off 3:off 4:off 5:off 6:off

quota_nld      0:off 1:off 2:off 3:off 4:off 5:off 6:off

rdisc          0:off 1:off 2:off 3:off 4:off 5:off 6:off

rdma          0:off 1:on 2:on 3:on 4:on 5:on 6:off

restorecond    0:off 1:off 2:off 3:off 4:off 5:off 6:off

rngd          0:off 1:off 2:off 3:off 4:off 5:off 6:off

rsyslog        0:off 1:off 2:on 3:on 4:on 5:on 6:off

saslauthd      0:off 1:off 2:off 3:off 4:off 5:off 6:off

smartd        0:off 1:off 2:off 3:off 4:off 5:off 6:off

sshd          0:off 1:off 2:on 3:on 4:on 5:on 6:off

svnserve      0:off 1:off 2:off 3:off 4:off 5:off 6:off

sysstat        0:off 1:on 2:on 3:on 4:on 5:on 6:off

udev-post      0:off 1:on 2:on 3:on 4:on 5:on 6:off

[root@oldboy init.d]# chkconfig crond status

chkconfig version 1.3.49.5 - Copyright (C) 1997-2000 Red Hat, Inc.

This may be freely redistributed under the terms of the GNU Public License.

usage:  chkconfig [--list] [--type ] [name]

        chkconfig --add

        chkconfig --del

        chkconfig --override

        chkconfig [--level ] [--type ]

[root@oldboy init.d]# chkconfig --list crond

crond          0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@oldboy init.d]# chkconfig crond off

[root@oldboy init.d]# chkconfig --list crond

crond          0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@oldboy init.d]# chkconfig crond on

[root@oldboy init.d]# chkconfig --list crond

crond          0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@oldboy init.d]# chkconfig --level 3 crond off

[root@oldboy init.d]# chkconfig --list crond

crond          0:off 1:off 2:on 3:off 4:on 5:on 6:off

[root@oldboy init.d]# chkconfig --level 45 crond off

[root@oldboy init.d]# chkconfig --list crond

crond          0:off 1:off 2:on 3:off 4:off 5:off 6:off

[root@oldboy init.d]# chkconfig --list rsyncd

service rsyncd does not support chkconfig

      For example, random.init has these three lines:

      # chkconfig: 2345 20 80

      # description: Saves and restores system entropy pool for \

      #              higher quality random number generation.

      This says that the random script should be started in levels 2, 3, 4, and 5, that its start  priority

      should  be  20,  and  that its stop priority should be 80.  You should be able to figure out what the

      description says; the \ causes the line to be continued.  The extra space in front  of  the  line  is

      ignored.

[root@oldboy init.d]# ll /etc/init.d/rsyncd

-rwxr-xr-x. 1 root root 763 May 30 09:55 /etc/init.d/rsyncd

[root@oldboy init.d]# chmod +x /etc/init.d/rsyncd

[root@oldboy init.d]# chkconfig --add rsyncd

[root@oldboy init.d]# chkconfig --list rsyncd

rsyncd        0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@oldboy init.d]# head -3 rsyncd

#!/bin/bash

# chkconfig: 2345 21 81

# description: startup rsync scripts

[root@oldboy init.d]# chkconfig rsyncd off

[root@oldboy init.d]# chkconfig --list rsyncd

rsyncd        0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@oldboy init.d]# chkconfig rsyncd on

[root@oldboy init.d]# chkconfig --list rsyncd

rsyncd        0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@oldboy init.d]# ls /etc/rc.d/rc3.d/|grep rsync

S21rsyncd

[root@oldboy init.d]# ls /etc/rc.d/rc3.d/-l|grep rsync

ls: cannot access /etc/rc.d/rc3.d/-l: No such file or directory

[root@oldboy init.d]# ls /etc/rc.d/rc3.d/ -l|grep rsync

lrwxrwxrwx. 1 root root 16 May 30 09:58 S21rsyncd -> ../init.d/rsyncd

[root@oldboy init.d]# chkconfig rsyncd off

[root@oldboy init.d]# ls /etc/rc.d/rc3.d/ -l|grep rsync

lrwxrwxrwx. 1 root root 16 May 30 09:59 K81rsyncd -> ../init.d/rsyncd

cat >/etc/systemd/system/oldboy_rsync.service<

[Unit]

Description=MySQL Server by oldboy

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=rsync

Group=rsync

ExecStart=/etc/init.d/rsyncd start

LimitNOFILE = 5000

EOF

systemctl start mysqld

systemctl enable mysqld

netstat -lntup|grep mysql

你可能感兴趣的:(5-29shell编程实战入门)