簡介
本文檔介紹自動執行郵件安全裝置中的匯入和匯出別名配置任務的步驟。
必要條件
需求
思科建議瞭解以下主題:
- 思科安全電子郵件閘道(SEG/ESA)AsyncOS 16.0.2
- 對雲裝置的命令列介面訪問
- Linux CLI
- Shell指令碼
採用元件
本檔案中的資訊是根據以下軟體:
本文中的資訊是根據特定實驗室環境內的裝置所建立。文中使用到的所有裝置皆從已清除(預設)的組態來啟動。如果您的網路運作中,請確保您瞭解任何指令可能造成的影響。
背景資訊
目標是實現某些任務的自動化,但某些流程通常需要手動干預。但是,在匯入和匯出當前別名配置時,可以完全自動執行這些任務,從而無需手動輸入。
匯出和匯入别名表
要匯入别名表,首先要驗證SSH和SCP訪問,以確保可以連線到電子郵件網關。
繼續操作之前,裝置內必須存在别名表:
(Machine esa1.xyz.iphmx.com) (SERVICE)> clustermode cluster; aliasconfig print
test: test@example.com, test@example2.com, test@example3.com
test2: test@domain.com, test@domain2.com, test@domain3.com
(Cluster Hosted_Cluster) (SERVICE)>
使用aliasconfig 命令的export子命令備份任何現有的别名表時,將生成一個檔案(使用您指定的名稱)並將其儲存在監聽程式的/configuration目錄中。
使用Bash指令碼匯出别名表
在這種情況下,bash指令碼會連線到您的CES裝置,並繼續匯出別名檔案
bash指令碼的結構如下:
#!/bin/bash
# Configuration of SSH keys and paths
PROXY_KEY="/full/path/folder/.ssh/id_rsa"
SECOND_KEY="/full/path/folder/.ssh/id_rsa"
LOCAL_PORT="2200"
PROXY_USER="dh-user"
PROXY_HOST="f4-ssh.iphmx.com"
TARGET_HOST="esa1.xyz.iphmx.com"
REMOTE_USER="local_server_user"
REMOTE_FILE="/configuration/filename.csv"
LOCAL_DIR="/full/path/folder/Downloads"
LOCAL_FILE_PATH="${LOCAL_DIR}/aliasconfig-file.csv"
# 1. Connect to the proxy and set up the SSH tunnel
echo "Establishing connection to the proxy..."
ssh -i "$PROXY_KEY" -l "$PROXY_USER" -N -f "$PROXY_HOST" -L "$LOCAL_PORT:${TARGET_HOST}:22"
if [ $? -ne 0 ]; then
echo "Error: Failed to establish connection to the proxy."
exit 1
fi
echo "Proxy connection established."
# Pause for 5 seconds before proceeding
sleep 5
# 2. Export the aliasconfig file from the remote system
echo "Exporting aliasconfig file from the remote system..."
ssh -i "$SECOND_KEY" "$REMOTE_USER"@127.0.0.1 -p "$LOCAL_PORT" 'clustermode cluster; aliasconfig export aliasconfig-file.csv' 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: Failed to export the aliasconfig file."
exit 1
fi
echo "Aliasconfig file successfully exported."
# Pause for 5 seconds before proceeding
sleep 5
# 3. Download the file to the local directory
echo "Downloading file to the local directory..."
scp -i "$SECOND_KEY" -P "$LOCAL_PORT" -O "$REMOTE_USER"@127.0.0.1:"$REMOTE_FILE" "$LOCAL_DIR" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: Failed to download the file to the local directory."
exit 1
fi
echo "File successfully downloaded to: $LOCAL_FILE_PATH"
# Pause for 5 seconds before finalizing
sleep 5
# Finalizing the script
echo "Process completed successfully."
exit 0
說明
1. — 配置SSH金鑰和路徑
- PROXY_KEY和SECOND_KEY:用於身份驗證的SSH私鑰檔案的完整路徑。在此案例中,兩個關鍵字都設定為相同的路徑。
- 範例:/full/path/folder/.ssh/id_rsa
- LOCAL_PORT:指定SSH隧道的本地埠(2200)。
- 代理使用者:用於連線到代理伺服器的使用者名稱。
- 代理主機(_H):代理伺服器的主機名。
- TARGET_HOST:目標主機的完全限定的域名(FQDN)已更新為esa1.xyz.iphmx.com。
- REMOTE_USER:用於通過SSH隧道連線到遠端裝置的使用者名稱。
- REMOTE_FILE:遠端系統上儲存匯出檔案的路徑(/configuration/filename.csv)。
- LOCAL_DIR:用於儲存檔案的本地目錄設定為/full/path/folder/Downloads。
- LOCAL_FILE路徑:下載檔案的完整本地路徑,更新為${LOCAL_DIR}/aliasconfig-file.csv。
2. — 連線到代理並設定SSH隧道
echo "Establishing connection to the proxy..."
ssh -i "$PROXY_KEY" -l "$PROXY_USER" -N -f "$PROXY_HOST" -L "$LOCAL_PORT:${TARGET_HOST}:22"
- 目的:
— 設定到代理伺服器的SSH隧道,以便與目標主機進行安全通訊。
- 更新:
- SSH私鑰現在位於/full/path/folder/.ssh/id_rsa。
— 目標主機名已更新為esa1.xyz.iphmx.com。
- 錯誤處理:
— 如果連線失敗,將顯示錯誤消息,指令碼退出並顯示錯誤代碼(exit 1)。
3. — 暫停5秒後繼續
- 目的:
— 引入延遲,以確保SSH隧道完全建立,然後繼續下一步。
4. — 從遠端系統匯出aliasconfig檔案
echo "Exporting aliasconfig file from the remote system..."
ssh -i "$SECOND_KEY" "$REMOTE_USER"@127.0.0.1 -p "$LOCAL_PORT" 'clustermode cluster; aliasconfig export aliasconfig-file.csv' 2>/dev/null
- 目的:
— 通過SSH隧道連線到目標主機,並將別名配置匯出到名為aliasconfig-file.csv的檔案。
- 更新:
— 匯出檔案的檔名已更新為aliasconfig-file.csv。
- 輸出重定向:
- 2>/dev/null禁止從SSH命令傳送任何錯誤消息。
- 錯誤處理:
— 如果匯出失敗,將顯示錯誤消息,指令碼將退出。
5. — 將檔案下載到本地目錄
echo "Downloading file to the local directory..."
scp -i "$SECOND_KEY" -P "$LOCAL_PORT" -O "$REMOTE_USER"@127.0.0.1:"$REMOTE_FILE" "$LOCAL_DIR" 2>/dev/null
- 目的:
— 使用scp將匯出檔案從遠端系統安全地複製到本地目錄。
- 更新:
— 本地檔案在/full/path/folder/Downloads目錄中另存為aliasconfig-file.csv。
- 錯誤處理:
— 如果檔案下載失敗,將顯示錯誤消息,指令碼將退出。
正在完成指令碼
echo "Process completed successfully."
exit 0
- 目的:
— 輸出成功消息並退出帶有成功代碼的指令碼(退出0),表示所有操作均已成功完成。
SSH金鑰
可以在指令碼中注意兩個變數PROXY_KEY和HOST_KEY。這些金鑰可以相同或不同。
PROXY_KEY用於連線到必須跳轉到CESA伺服器的代理雲。
HOST_KEY是用於以本地使用者身份登入的金鑰,無需密碼。
附註:要配置對CES代理的SSH訪問並為裝置上的本地使用者設定SSH金鑰,請參閱配置指南。
驗證匯出的檔案
執行匯出指令碼後,您可以驗證其內容,並發現其中包含的資訊與裝置aliasconfig CLI命令中顯示的源指令碼相同。
$ pwd
/full/path/folder/Downloads
$ ls
filename.csv
$ cat filename.csv
# File exported by the CLI at 20250702T125347
test: test@example.com, test@example2.com, test@example3.com
test2: test@domain.com, test@domain2.com, test@domain3.com⏎
使用Bash指令碼匯入别名表
匯出當前別名檔案後,您可以對其進行修改,新增必要的條目,然後將其匯入ESA alia配置。
匯入bash指令碼的結構如下:
#!/bin/bash
# Configuration of SSH keys and paths
SSH_KEY="/full/path/folder/.ssh/id_rsa"
LOCAL_PORT="2200"
REMOTE_USER="local_server_user"
LOCAL_FILE="/full/path/folder/Downloads/new-filename.csv"
OUTPUT_DIR="/full/path/folder/Downloads"
# Get the current local date in the desired format
CURRENT_DATE=$(date +"%Y-%m-%d_%H-%M-%S")
# 1. Upload the new aliasconfig file
echo "Uploading new aliasconfig file to the remote system..."
scp -i "$SSH_KEY" -P "$LOCAL_PORT" -O "$LOCAL_FILE" "$REMOTE_USER"@127.0.0.1:/configuration 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: Failed to upload the aliasconfig file."
exit 1
fi
echo "Aliasconfig file successfully uploaded."
# Pause for 5 seconds before proceeding
sleep 5
# 2. Import the new aliasconfig file and commit with a comment
COMMIT_COMMENT="Importing new entries to aliasconfig - $CURRENT_DATE"
echo "Importing the new aliasconfig file and committing changes..."
ssh -i "$SSH_KEY" "$REMOTE_USER"@127.0.0.1 -p "$LOCAL_PORT" "clustermode cluster; aliasconfig import new-filename.csv; commit \"$COMMIT_COMMENT\"" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: Failed to import the aliasconfig file or commit changes."
exit 1
fi
echo "Aliasconfig file successfully imported and committed with comment: '$COMMIT_COMMENT'."
# Pause for 5 seconds before proceeding
sleep 5
# 3. Print the current aliasconfig and save it to a new file
OUTPUT_FILE="${OUTPUT_DIR}/current-aliasconfig-${CURRENT_DATE}.txt"
echo "Printing current aliasconfig and saving it to: $OUTPUT_FILE..."
ssh -i "$SSH_KEY" "$REMOTE_USER"@127.0.0.1 -p "$LOCAL_PORT" 'clustermode cluster; aliasconfig print' > "$OUTPUT_FILE" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: Failed to print the current aliasconfig."
exit 1
fi
echo "Current aliasconfig successfully saved to: $OUTPUT_FILE"
# Finalizing the script
echo "Process completed successfully."
exit 0
說明
1.- SSH路徑和金鑰配置
- SSH_KEY:SSH私鑰檔案的路徑,用於針對遠端伺服器進行安全身份驗證。
- LOCAL_PORT:指定用於SSH隧道的本地埠。
- REMOTE_USER:用於遠端伺服器上的身份驗證的使用者帳戶。
- LOCAL_FILE:要匯入的aliasconfig CSV檔案的本地路徑。
- OUTPUT_DIR:匯入過程後儲存當前配置副本的本地資料夾。
2. — 獲取當前日期和時間
CURRENT_DATE=$(date +"%Y-%m-%d_%H-%M-%S")
- 目的:
— 以特定格式儲存當前日期和時間,以用於檔名和註釋。
- 更新:
— 通過時間戳輕鬆跟蹤和組織日誌和備份。
3. — 將新的aliasconfig檔案上傳到遠端ESA伺服器
以下是aliasconfig檔案的新內容:
# File exported by the CLI at 20250709T112719
test: new-data@example.com, new-date@example2.com, new-date@example3.com
test2: new-date@domain.com, new-data@domain2.com, new-data@domain3.com⏎
請繼續說明以上傳檔案:
echo "Uploading new aliasconfig file to the remote system..."
scp -i "$SSH_KEY" -P "$LOCAL_PORT" -O "$LOCAL_FILE" "$REMOTE_USER"@127.0.0.1:/configuration 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: Failed to upload the aliasconfig file."
exit 1
fi
echo "Aliasconfig file successfully uploaded."
- 目的:
— 使用SCP over SSH將aliasconfig CSV檔案從本地電腦傳輸到遠端伺服器上的/configuration目錄。
- 更新:
— 如果上載失敗,指令碼將顯示錯誤並停止以防止匯入不完整。
4. — 匯入新的aliasconfig檔案並使用註釋提交
COMMIT_COMMENT="Importing new entries to aliasconfig - $CURRENT_DATE"
echo "Importing the new aliasconfig file and committing changes..."
ssh -i "$SSH_KEY" "$REMOTE_USER"@127.0.0.1 -p "$LOCAL_PORT" "clustermode cluster; aliasconfig import new-filename.csv; commit \"$COMMIT_COMMENT\"" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: Failed to import the aliasconfig file or commit changes."
exit 1
fi
echo "Aliasconfig file successfully imported and committed with comment: '$COMMIT_COMMENT'."
- 目的:
— 通過SSH連線,將上載的CSV檔案匯入別名配置,並使用帶有時間戳的註釋提交更改以進行跟蹤。
- 更新:
— 如果匯入或提交失敗,將顯示一條錯誤消息,指令碼將退出以保持配置一致。
5. — 列印當前aliasconfig並將其儲存到新的本地檔案中
OUTPUT_FILE="${OUTPUT_DIR}/current-aliasconfig-${CURRENT_DATE}.txt"
echo "Printing current aliasconfig and saving it to: $OUTPUT_FILE..."
ssh -i "$SSH_KEY" "$REMOTE_USER"@127.0.0.1 -p "$LOCAL_PORT" 'clustermode cluster; aliasconfig print' > "$OUTPUT_FILE" 2>/dev/null
if [ $? -ne 0 ]; then
echo "Error: Failed to print the current aliasconfig."
exit 1
fi
echo "Current aliasconfig successfully saved to: $OUTPUT_FILE"
- •目的:
— 通過SSH連線,列印當前別名配置,並將輸出儲存到本地帶有時間戳的檔案以進行備份和稽核。
- 更新:
— 如果操作失敗,指令碼將顯示錯誤並停止,以避免給出不完整的結果。
驗證更改
執行指令碼後,您可以驗證別名配置表中的更改,並檢查系統日誌中的提交。
驗證新的aliasconfig表條目
新更改已應用於該表。
(Machine esa1.xyz.iphmx.com) (SERVICE)> clustermode cluster; aliasconfig print
test: new-data@example.com, new-data@example2.com, new-data@example3.com
test2: new-data@domain.com, new-data@domain2.com, new-data@domain3.com
驗證提交更改
此命令使您能夠跟蹤和檢視提交到ESA的變更,包括進行變更的使用者及其發生日期。
(Machine esa1.xyz-66.iphmx.com) (SERVICE)> grep "commit" system_logs
Wed Jul 9 11:29:42 2025 Info: PID 95790: User local_server_user commit changes: Importing new entries to aliasconfig - 2025-07-09_11-29-15
指令碼靈活性
雖然此指令碼當前使用Bash編寫,但可以輕鬆地改編或用其他指令碼或程式語言(如Python、PowerShell或Perl)重新編寫,以便更好地適應不同管理員和環境的首選項或要求。這種靈活性可確保能夠維護核心邏輯和工作流程,同時利用與您的操作要求最接近的語言或工具。
最終想法
此匯入/匯出指令碼為直接在裝置上管理別名配置提供了實用而高效的解決方案。通過自動上傳、匯入和備份配置檔案,管理員可以安全可靠地引入更改,而無需手動干預。該指令碼不僅簡化了過程,而且還通過帶有時間戳的備份和提交註釋確保了可跟蹤性。
此外,擁有此類指令碼還有助於在您的環境中保持一致性和合規性,特別是在需要多次更改或批次更新時。定期備份當前配置可增加一層安全保護,在需要時可快速恢復或回滾。
總之,此方法使各團隊能夠更有信心、更有控制力和更高效率地管理配置更新。對於任何將來的調整,指令碼可以很容易地適應處理其他型別的配置檔案,或根據需要進一步自動執行其他維護任務。
參考連結