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

    












發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章