Hero Image
Shell 腳本學習筆記

Shell 腳本學習筆記 執行算術運算 val=`expr $a + $b` 運算符 符號 說明 示例 ! 非運算 [ ! false ] -o 或運算 [ $a -lt 20 -o $b -gt 20 ] -a 與運算 [ $a -lt 20 -a $b -gt 20 ] = 相等檢測 [ $a = $b ] != 不相等檢測 [ $a != $b ] -z 字串長度是否為 0,為 0 則回傳 true [ -z $a ] -n 字串長度不為 0,不為 0 回傳 true [ -n $a ] str 檢測字串是否為空,不為空回傳 true [ $a ] -b 檢測檔案是否是區塊裝置檔 [ -b $file ] -c 檢測檔案是否是字元裝置 .. -d 檢測檔案是否為目錄 [ -d $file ] -f 檢測檔案是否為一般檔案 [ -f $file ] -r 檢測檔案是否可讀 .. -w 檢測檔案是否可寫 .. -x 檢測檔案是否可執行 .. -s 檢測檔案是否為空 .. -e 檢測檔案是否存在 .. 特殊變數 變數 含義 $0 目前腳本的檔名 $n 傳遞給腳本或函式的參數,n 表示第幾個參數 $# 傳遞給腳本或函式的參數個數 $* 傳遞給腳本或函式的所有參數,所有參數視為一個詞,例如 “1 2 3” $@ 傳遞給腳本或函式的所有參數,每個參數視為一個詞,用雙引號包含,例如 “1” “2” “3” $? 上個命令的退出狀態,或函式的回傳值 $$ 目前 Shell 行程 ID;對 Shell 腳本而言,就是該腳本所在的行程 ID POSIX 程式退出狀態 狀態碼 含義 0 命令成功退出 > 0 在重導向或單詞展開期間(~、變數、命令、算術展開以及單詞切割)失敗 1 - 125 命令不成功退出,各命令自行定義特定退出值含義 126 命令找到但檔案無法執行 127 命令未找到 > 128 命令因收到信號而終止 輸入輸出重導向 命令 說明 command > file 將輸出重導向到 file command > file 將輸出以追加的方式重導向到 file n > file 將檔案描述符為 n 的檔案重導向到 file n » file 將檔案描述符為 n 的檔案以追加的方式重導向到 file n >& m 將輸出檔案 m 和 n 合併 n <& m 將輸入檔案 m 和 n 合併 « tag 將開始標記 tag 和結束標記 tag 之間的內容作為輸入 檔案包含 使用 . 或 source 包含檔案

Hero Image
openresty+redis 攔截高頻訪問 IP

openresty+redis 攔截高頻訪問 IP init_by_lua_block { redis = require "redis" client = redis.connect('127.0.0.1', 6379) } server { listen 8080; location / { access_by_lua_file /usr/local/nginx/conf/lua/block.lua; proxy_pass http://192.168.1.102:8000; } } -- Redis-based IP rate limiting / blocking for OpenResty (ngx_lua) -- NOTE: -- This script assumes a global `client` variable is used/stored. -- Make sure `redis` module is available and `client` is initialized somewhere. local function isConnected() return client:ping() end local function createRedisConnection() return redis.connect("127.0.0.1", 6379) end -- 如果發生 redis 連線失敗,將停止攔截(直接放行) if pcall(isConnected) then -- already connected (or ping succeeded) else -- not connected; try reconnect if pcall(createRedisConnection) then -- 斷開重連:會導致每次訪問都需要重連 redis -- 訪問量大時建議:關閉重連邏輯(pcall 不執行),直接 ngx.exit 放行/終止 client = createRedisConnection() else ngx.exit(ngx.OK) end end local ttl = 60 -- 監測週期(秒) local bktimes = 30 -- 在監測週期內達到觸發攔截的訪問量 local block_ttl = 600 -- 觸發攔截後攔截時間(秒) local ip = ngx.var.remote_addr local ipvtimes = client:get(ip) if ipvtimes then if ipvtimes == "-1" then -- blocked return ngx.exit(403) else local last_ttl = client:ttl(ip) -- ngx.say("key exist.ttl is ", last_ttl) if last_ttl == -1 then client:set(ip, 0) client:expire(ip, ttl) -- ngx.say("ttl & vtimes recount") return ngx.exit(ngx.OK) end local vtimes = tonumber(client:get(ip)) + 1 if vtimes < bktimes then client:set(ip, vtimes) client:expire(ip, last_ttl) -- ngx.say(ip, " view ", vtimes, " times") return ngx.exit(ngx.OK) else -- ngx.say(ip, " will be block next time.") client:set(ip, -1) client:expire(ip, block_ttl) return ngx.exit(ngx.OK) end end else -- key does not exist client:set(ip, 1) -- ngx.say(ip, " view 1 times") client:expire(ip, ttl) return ngx.exit(ngx.OK) end