shell scripts学习(三)

1. netstat命令

可查询当前主机所有开启的网络服务端口.

[root@linux ~]# netstat  -tuln

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:43975           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:51886           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:45903           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp       0      0 :::
80                  :::*                    LISTEN     
tcp       0      0 :::22                   :::*                    LISTEN     
tcp       0      0 ::
25                 :::*                    LISTEN     
udp        0      0 0.0.0.0:68              0.0.0.0:*                          
udp        0      0 0.0.0.0:69              0.0.0.0:*                          
udp        0      0 0.0.0.0:111             0.0.0.0:*                          
udp        0      0 192.168.98.255:137      0.0.0.0:*    

说明:

-t---------------->tcp

-u--------------->udp

-l---------------->listening

-n--------------->numberic   

80--------------->www

22--------------->ssh

21---------------->ftp

25---------------->mail

ex:

通过netstat来侦测主机是否开启了四个网络服务端口

[root@linux ~]# vim sh09.sh

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

export PATH

echo "Now, the services of your Linux system will be detect!"

echo -e "The www, ftp, ssh, and mail will be detect! \n"

testing='netstat -tuln | grep ":80"'

if [ "$testing" != "" ]; then

echo "WWW is running in your system"

fi

testing='netstat -tuln | grep ":22"'

if [ "$testing" != "" ]; then

echo "SSH is running in your system"

fi

testing='netstat -tuln | grep ":21"'

if [ "$testing" != "" ]; then

echo "FTP is running in your system"

fi

testing='netstat -tuln | grep ":25"'

if [ "$testing" != "" ]; then

echo "Mail is running in your system"

fi

2. 判断语句case......in...esac

判断变量有多个不同内容的情况

语法格式:

case $变量名称 in

"变量内容1")

程序段

;; #------------------------->用两个分号代表程序段的结束

"变量内容2")

程序段

;;

*) #----------------------->其他变量内容

其他程序段

exit 1

;;

esac

3. shell scripts中的函数功能

function的语法格式:

function func_name() {

程序段

}

ex:

[root@linux ~]# vim sh11-2.sh

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

export PATH

function printit() { #-------->函数名自定义

echo  -n  "Your choice is"

}

echo "This program will print your selection!"

case $1 in

"one")

printit; echo $1 | tr 'a-z' 'A-Z'

;;

"two")

printit; echo $1 | tr 'a-z' 'A-Z'

;;

"three")

printit; echo $1 | tr 'a-z' 'A-Z'

;;

*)

echo "Usage {one|tow|three}"

;;

esac

4. function拥有的内建变量($0, $1, $2, $3, .......),说白了也就是给函数传参

    $0------------->代表了函数的名字

    $1,$2...------->代表了给函数传的参数

    ex:

[root@linux ~]# vim sh11-3.sh

#!/bin/bash

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

export PATH

function printit() { #-------->函数名自定义

echo  -n  "Your choice is $1"

}

echo "This program will print your selection!"

case $1 in

"one")

printit 1 #------------->传入参数1

;;

"two")

printit 2 #------------>传入参数2

;;

"three")

printit 3 #------------->传入参数3

;;

*)

echo "Usage {one|tow|three}"

;;

esac

    












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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值