shell scripts学习(三)

2014-11-24 03:31:56 · 作者: · 浏览: 0

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