a

automation

Last active 5 hours ago automation certificate script shell ssl

將 server、中間、根憑證合併成完整憑證鏈,輸出固定檔名或自動加日期的檔案,方便伺服器直接使用。

0 0 0 1
1 # 將伺服器證書、中間證書和根證書合併成一個完整的證書鏈文件
2 # server-cert.pem: 伺服器證書,通常由憑證頒發機構 (CA) 簽發,包含伺服器的公鑰和身份資訊
3 # intermediate-ca.pem: 中間證書,由中間 CA 簽發,用於建立伺服器證書與根證書之間的信任鏈
4 # root-ca.pem: 根證書,由根 CA 簽發,是信任鏈的頂端(通常客戶端已預裝根證書,此處包含僅為完整性)
5 # fullchain.pem: 輸出文件,包含完整的證書鏈,從伺服器證書到根證書,供伺服器(如 Nginx 或 Apache)使用
6 cat server-cert.pem intermediate-ca.pem root-ca.pem > fullchain.pem
7
8
9
10 # 將伺服器證書、中間證書和根證書合併成一個完整的證書鏈文件,並以當前日期作為文件名的一部分

自動檢測 macOS 並設置 iTerm2 偏好設定資料夾為自訂路徑。

0 0 0 1
1 #!/bin/sh
2
3 # 取得作業系統類型(例如 Darwin 表示 macOS)
4 system_type=$(uname -s)
5
6 # 如果是 macOS 系統,才執行以下區塊
7 if [ "$system_type" = "Darwin" ]; then
8
9 # 這裡可以考慮加入使用 Homebrew 安裝 iTerm2 的指令(例如 brew install --cask iterm2)
10 # 不過目前尚未實作

此程式碼用於取得目前 Python 環境中所有已安裝的套件名稱和版本,並將結果整理成 CSV 檔案,方便進行記錄、分享或系統排查。

0 0 0 1
1 # 引入需要用到的套件
2 import pandas as pd # 用來處理資料,還能幫我們存成 CSV 檔
3 from pkg_resources import working_set # 用來拿到所有已安裝的套件清單
4
5 # 步驟 1: 把所有已安裝的套件名稱和版本抓出來,直接變成 Pandas DataFrame
6 # 用列表生成式搭配 DataFrame,省去不必要的中間變數
7 df_packages = pd.DataFrame(
8 [(pkg.key, pkg.version) for pkg in working_set],
9 columns=["Package", "Version"]
10 ).sort_values(by="Package")

subprocess 模組可用於執行系統指令、與外部程式互動,適用於自動化腳本、批次處理與系統管理等應用。

0 0 0 1
1 import subprocess
2
3 # 執行系統指令並獲取輸出
4 result = subprocess.run(["ls", "-l"], capture_output=True, text=True)
5
6 # 顯示執行結果
7 print("命令輸出:")
8 print(result.stdout)
9
10 # 檢查是否執行成功

shutil 模組提供高級的檔案與目錄管理功能,包括複製、移動、壓縮與刪除,適用於備份、部署與自動化檔案管理。

0 0 0 1
1 import shutil
2 import os
3
4 # 定義來源與目標
5 source_file = "example.txt"
6 destination_dir = "backup/"
7 destination_file = os.path.join(destination_dir, source_file)
8
9 # 確保目標目錄存在
10 os.makedirs(destination_dir, exist_ok=True)
Last active 5 hours ago automation python threading timer webbrowser

此程式透過 threading.Timer 延遲指定秒數後,自動開啟指定的 URL。適用於自動化測試、提醒功能或延遲啟動特定網頁,確保主程式不中斷的情況下執行非同步操作。

0 0 0 1
1 import threading
2 import webbrowser
3
4 def open_url(url):
5 """ 開啟指定的 URL """
6 webbrowser.open(url, new=2)
7
8 # 設定延遲時間(秒)
9 delay = 2
10 url = "https://www.example.com"
Last active 5 hours ago argparse automation cli command-line parser python

這段程式碼使用 Python 的 argparse 模組來解析命令列參數。它支援必填的位置參數(輸入檔案路徑)、選填的輸出檔案路徑(-o 或 --output),以及一個開關參數(-v 或 --verbose)來啟用詳細模式。這適用於 CLI 工具,使得用戶可以透過命令列提供不同的參數來控制程式行為。

0 0 0 1
1 import argparse
2
3 # 建立參數解析器物件
4 parser = argparse.ArgumentParser(description='這是一個範例程式,使用 argparse 模組解析命令列參數')
5
6 # 添加位置參數
7 parser.add_argument('input_file', help='輸入檔案的路徑')
8
9 # 添加選擇性參數
10 parser.add_argument('-o', '--output', help='輸出檔案的路徑')

這段程式碼適合用於開發或部署 Streamlit 應用時,確保運行的 Python 版本符合 Streamlit 的支援範圍,避免因版本不符而導致問題。

0 0 0 1
1 import sys
2
3 class PythonVersionChecker:
4 def __init__(self, supported_versions):
5 self.supported_versions = supported_versions
6
7 def get_current_version(self):
8 """取得當前的 Python 版本。"""
9 return sys.version_info[:2]