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 ""' \; |
timmy / 更改 Linux 主機名稱並更新 /etc/hosts
1 likes
0 forks
1 files
Last active 10 months ago
這段 Bash 腳本用於更改系統的主機名稱:它先取得目前的主機名稱並從 /etc/hosts 中移除該名稱的條目,接著使用 hostnamectl 指令設定新的主機名稱,最後將新主機名稱加入到 /etc/hosts 文件中,並輸出成功更新的提示訊息。
| 1 | #!/bin/bash |
| 2 | |
| 3 | # 請將 'new_hostname' 替換為你要設定的新主機名稱 |
| 4 | NEW_HOSTNAME="new_hostname" |
| 5 | |
| 6 | # 獲取原本的主機名稱 |
| 7 | OLD_HOSTNAME=$(hostname) |
| 8 | |
| 9 | # 刪除 /etc/hosts 中原本的主機名稱 |
| 10 | sudo sed -i "/127.0.0.1 $OLD_HOSTNAME/d" /etc/hosts |
timmy / 查詢外部 IP 位址並設定環境變數
0 likes
0 forks
1 files
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 / Dnsmasq 設定與網路管理
0 likes
0 forks
1 files
Last active 10 months ago
| 1 | bogus-priv # Block fake private IP responses |
| 2 | no-resolv # Ignore /etc/resolv.conf for upstream DNS |
| 3 | dns-forward-max=150 # Limit parallel DNS queries to 150 |
| 4 | clear-on-reload # Clear cache when dnsmasq reloads |
| 5 | domain-needed # Ignore queries without a domain name |
| 6 | no-negcache # Do not cache negative (non-existent) DNS responses |
| 7 | no-poll # Do not poll /etc/resolv.conf for changes |
| 8 | strict-order # Use upstream DNS servers in the order they are listed |
| 9 | |
| 10 | # AdGuard DNS 封鎖廣告和追蹤器。 |
Last active 10 months ago
| 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" |
timmy / 強化的 iptables 防火牆規則設定
0 likes
0 forks
1 files
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 / 清理未使用的 SSH 暫存檔案
0 likes
0 forks
1 files
Last active 10 months ago
這個 Bash 腳本用於 清理 /tmp 目錄中無效的 SSH 相關暫存檔案。它會搜尋所有以 ssh- 開頭的檔案或目錄,使用 lsof 檢查是否有行程正在使用它們,若未被使用則刪除,以確保 /tmp 目錄保持乾淨。這適用於 系統維護、提升伺服器安全性,防止長時間未使用的 SSH 暫存文件佔用空間。
| 1 | #!/bin/bash |
| 2 | |
| 3 | # 定義要檢查的目錄 |
| 4 | TMP_DIR="/tmp" |
| 5 | |
| 6 | # 查找所有以 ssh- 開頭的文件或目錄 |
| 7 | for file in "$TMP_DIR"/ssh-*; do |
| 8 | # 如果沒有匹配的文件,跳過 |
| 9 | if [ ! -e "$file" ]; then |
| 10 | continue |
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 / 建立並配置新使用者帳戶(含 sudo 權限)
0 likes
0 forks
1 files
Last active 10 months ago
這個 Bash 腳本用於根據輸入的使用者名稱,建立一個新的使用者帳號,並提示輸入密碼後設定該密碼,然後將該使用者加入 sudo 群組,同時更新 sudoers 檔案以允許該使用者無密碼執行 sudo 指令,方便日後進行系統管理。
| 1 | #!/bin/bash |
| 2 | |
| 3 | # Check if a username was provided as an argument |
| 4 | if [ $# -ne 1 ]; then |
| 5 | echo "Error: Please provide a username as an argument." |
| 6 | exit 1 |
| 7 | fi |
| 8 | |
| 9 | # Get the username from the argument |
| 10 | username="$1" |