timmy / 批量顯示 Python 檔案內容

0 likes
0 forks
1 files
Last active 10 months ago
這段 Bash 指令使用 find 指令在當前目錄(.)及其子目錄中 搜尋所有 .py(Python)檔案,並對每個找到的檔案執行 顯示檔名並輸出其內容。這適用於 快速查看專案中的 Python 檔案,方便 程式碼審查、備份或檢查腳本內容。
1 #!/bin/bash
2 find . -name "*.py" -exec sh -c 'echo "=== {} ==="; cat "{}"; echo ""' \;
Last active 10 months ago
這段 Bash 腳本設定了基本的環境變數,然後使用 curl 指令從 ifconfig.me 取得目前裝置的 公網 IP 位址,並將結果顯示在終端機。這適用於 快速查詢外部 IP、網路偵錯、伺服器監控,特別是在 NAT 環境或動態 IP 網路 中檢測目前外部 IP 是否變更。
1 #!/bin/bash
2
3 # 定義預設的配置設定
4 SHELL=/bin/sh
5 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
6 TZ='Asia/Taipei'
7
8 # 將配置設定寫入環境變數
9 export PATH
10 export LANG=en_US.UTF-8

timmy / 系統日誌清理與維護

0 likes
0 forks
1 files
Last active 10 months ago
這個 Bash 腳本用於 清理 Linux 系統的日誌檔案,主要包括 身份驗證日誌 (auth.log)、登入歷史 (wtmp, btmp, lastlog)、系統日誌 (syslog)、郵件日誌 (mail.log),並刪除舊的壓縮日誌 (.gz) 以釋放空間。此外,它會 重設失敗登入記錄 (faillog),確保 lastlog 權限正確,適用於 系統維護、隱私保護及釋放伺服器磁碟空間。
1 #!/bin/bash
2
3 # Define log file paths
4 AUTH_LOG="/var/log/auth.log"
5 BTMP_LOG="/var/log/btmp"
6 WTMP_LOG="/var/log/wtmp"
7 LASTLOG="/var/log/lastlog"
8 SYSLOG="/var/log/syslog"
9 MAIL_LOG="/var/log/mail.log"
Last active 10 months ago
這個 iptables 防火牆腳本 用於 強化伺服器的網路安全,透過 允許合法流量(如 SSH、HTTP/HTTPS)並封鎖未授權連線,同時防禦各種攻擊(如 暴力破解、SYN Flood、埠掃描、DoS)。此外,它會 記錄異常流量 以便後續分析,並將規則儲存至 /etc/iptables/rules.v4,確保設定在重啟後仍生效。適用於 企業伺服器、內部網路防護及個人伺服器安全強化。
1 #!/bin/bash
2
3 # === Basic Settings ===
4
5 # Clear all existing rules
6 iptables -F
7 iptables -X
8 iptables -Z
9
10 # Set default policies: drop all incoming and forwarding traffic, allow outgoing

timmy / 使用 ipset 建立台灣 IP 位址集

0 likes
0 forks
1 files
Last active 10 months ago
這個 Bash 腳本用於 建立並更新 ipset 規則,將台灣的 IP 網段加入名為 taiwan_ips 的集合,以便在 iptables 防火牆 中使用。它會從 GitHub 下載最新的 台灣 IP 清單 (tw.txt),然後解析其中的 IPv4 網段 並加入 ipset,最後可用於 限制 SSH 存取、流量過濾或強化網路安全,適用於 伺服器管理、防禦異地登入或阻擋非台灣 IP 連線。
1 #!/bin/bash
2
3 # Delete the existing taiwan_ips ipset collection if it exists
4 ipset destroy taiwan_ips
5
6 # Create a new taiwan_ips ipset collection
7 ipset create taiwan_ips hash:net
8
9 # Download the Taiwan IP range file using wget (commented out)
10 wget https://github.com/Loyalsoldier/geoip/raw/release/text/tw.txt -O tw.txt

timmy / Bash 陣列遍歷與輸出

0 likes
0 forks
1 files
Last active 10 months ago
這個 Bash 腳本建立了一個數字陣列 arr,並透過 for 迴圈遍歷陣列中的每個元素,使用 printf 將其逐行輸出。這適用於 處理資料集合、批次操作或腳本自動化,可用於 列印資料、執行批次指令或進一步處理陣列元素。
1 #!/bin/bash
2
3 # 建立一個陣列
4 arr=(1 2 3 4 5)
5
6 # 迴圈遍歷陣列的每一個元素
7 for i in "${arr[@]}"; do
8 # 處理每一個元素
9 # echo $i
10 printf "%s\n" "$i"

timmy / IP 位址查詢與解析

0 likes
0 forks
1 files
Last active 10 months ago
這個 Bash 腳本用於 查詢目前裝置的公網 IP 及其地理資訊。它會先檢查系統是否安裝了 curl 和 jq(jq 用於解析 JSON),然後使用 curl 向 ip-api.com 發送請求,獲取目前裝置的 IP 位址、國家、城市、ISP、時區等資訊,並透過 jq 以可讀格式輸出。這適用於 網路診斷、IP 追蹤、地理位置查詢或伺服器網路狀態檢測。
1 #!/bin/bash
2
3 SHELL=/bin/sh
4 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
5 TZ="Asia/Taipei"
6 export PATH
7 export LANG=en_US.UTF-8
8 export LANGUAGE=en_US:en
9
10 # Check if curl is installed
Newer Older