開發檢測URL指令碼,檢測url指令碼

來源:互聯網
上載者:User

開發檢測URL指令碼,檢測url指令碼

大家好,今天給大家帶來的是一些小執行個體,編寫檢測網站的指令碼,在這裡我用了兩種思路來實現。

1.第一種實現的如下:

2.第二種實現的如下:

第一種實現的指令碼如下:

#!/bin/bash# DATE:2018-2-20# URL:https://blog.csdn.net/qq_39591494# Email:www.ywyankai.cn# information:Website monitoring# By.yankerp. /etc/init.d/functionsfunction Connection(){        wget --spider -q -o /dev/null --tries=1 -T 5 https://$1if [ "$?" -eq 0 ]   then     action "https://$1 Monitoring success!" /bin/true   else     action "https://$1 Failure of monitoring!" /bin/falsefi}webaddress="www.baidu.comwww.jd.comwww.taobao.comwww.123haha.comwww.090909.comwww.627ywndw.comwww.aiqiyi.comwww.nginx.orgwww.apache.org"function main(){for U in $webaddressdo        Connection $Udone}main

剖析:

#!/bin/bash       #前面的注釋這裡不在做介紹了# DATE:2018-2-20# URL:https://blog.csdn.net/qq_39591494# Email:www.ywyankai.cn# information:Website monitoring# By.yankerp. /etc/init.d/functions      #這裡是載入函數庫,主要是為了後面的一些特效顏色function Connection(){????????#定義連結函數        wget --spider -q -o /dev/null --tries=1 -T 5 https://$1 #在這裡我們使用了wget 這裡參數不在解釋,在後面使用了$1特殊的位置變數 來判斷使用者輸入的內容if [ "$?" -eq 0 ]   #那麼 如果以上wget 執行成功了   then???????#那麼     action "https://$1 Monitoring success!" /bin/true????#使用action來輸出https://$1使用者輸入的網站,  輸出成功   else     action "https://$1 Failure of monitoring!" /bin/false????#否則輸出失敗fi}webaddress="????在這裡我們定義變數,等於是網站位址集區www.baidu.com????#這裡有百度等網址,相當於 webaddress是變數名 那麼下面的網址就是變數的值 這些網址會賦值給變數名webaddresswww.jd.comwww.taobao.comwww.123haha.comwww.090909.comwww.627ywndw.comwww.aiqiyi.comwww.nginx.orgwww.apache.org"function main(){????#定義總函數for U in $webaddress????#這裡使用for迴圈 U是變數名 那麼$webaddress是變數名 它的值就是web網站 也就是說 把$webaddress賦值給U這個變數do???????#do        Connection $U????#在這裡調用函數Connection 這個函數的指令 大家可以看上面是wget操作 那麼$U調用變數 這個變數的值就是網址done????#最後結束for迴圈}main????#調用main總函數使整個指令碼運行

運行如下:

[root@shell-yankerp shell]# sh URLA.sh https://www.baidu.com Monitoring success!                   [  確定  ]https://www.jd.com Monitoring success!                      [  確定  ]https://www.taobao.com Monitoring success!                  [  確定  ]https://www.123haha.com Failure of monitoring!              [失敗]https://www.090909.com Failure of monitoring!               [失敗]https://www.627ywndw.com Failure of monitoring!             [失敗]https://www.aiqiyi.com Failure of monitoring!               [失敗]https://www.nginx.org Monitoring success!                   [  確定  ]https://www.apache.org Monitoring success!                  [  確定  ][root@shell-yankerp shell]# 

 

第二種方式指令碼內容如下:

#!/bin/bash. /etc/init.d/functionsRED_COLOR='\E[1;31m'  #紅GREEN_COLOR='\E[1;32m' #綠YELOW_COLOR='\E[1;33m' #黃BLUE_COLOR='\E[1;34m'  #藍PINK='\E[1;35m'      #粉紅RES='\E[0m'echo -en "${RED_COLOR}==========歡迎使用檢測指令碼輸入網址即可==========\n${RES}"function panduan(){        echo -en "${YELOW_COLOR}請您輸入你要檢測的網站:${RES}"                read NUM        if [ -z $NUM ];then           action "請您輸入一個網址!!!" /bin/false           exit 1        fi}function URL_check(){echo -en "${BLUE_COLOR}=================開始檢測請稍等============================\n${RES}"        wget --spider -q -o /dev/null --tries=1 -T 5 https://$NUM                if [ "$?" -eq 0 ]                   then                     action "https://$NUM 檢測成功" /bin/true                   else                     action "https://$NUM 檢測失敗" /bin/false                fi}function main(){while :do        panduan        URL_checkdone}main

剖析:

#!/bin/bash. /etc/init.d/functionsRED_COLOR='\E[1;31m'  #紅????#定義紅色變數 以此類推這裡不在介紹GREEN_COLOR='\E[1;32m' #綠YELOW_COLOR='\E[1;33m' #黃BLUE_COLOR='\E[1;34m'  #藍PINK='\E[1;35m'      #粉紅RES='\E[0m'echo -en "${RED_COLOR}==========歡迎使用檢測指令碼輸入網址即可==========\n${RES}"????#使用紅色欄位輸出歡迎使用此指令碼function panduan(){????#定義判斷函數        echo -en "${YELOW_COLOR}請您輸入你要檢測的網站:${RES}"????#使用黃色欄位輸入請您輸入檢測網站                read NUM????#等待使用者輸入 並賦值給變數NUM        if [ -z $NUM ];then????#判斷$NUM 長度是否為0            action "請您輸入一個網址!!!" /bin/false           exit 1        fi}function URL_check(){????#定義檢測指令碼echo -en "${BLUE_COLOR}=================開始檢測請稍等============================\n${RES}"????#使用藍色輸出開始檢測        wget --spider -q -o /dev/null --tries=1 -T 5 https://$NUM????#去連結下載                if [ "$?" -eq 0 ]????#如果連結下載成功了                   then????#那麼                     action "https://$NUM 檢測成功" /bin/true???#輸出檢測成功,以此類推                   else                     action "https://$NUM 檢測失敗" /bin/false                fi}function main(){????#定義總函數mainwhile :????#使用while迴圈 這裡條件一直為真do????????????        panduan????#迴圈panduan函數        URL_check????#以及連結函數done????#done}main????#最後調用main總函數使整個指令碼運行

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.