[英]Execute shell script in remote machine using Jenkins
我的要求是這樣的:詹金斯安裝在一台服務器上(比如服務器 A)。 我在此服務器中有 3 個腳本,用於將戰爭部署到不同的服務器中,例如B 和 C 。
有一個主腳本說JenkinsMainScript.sh 。 在此腳本中,我已根據執行 shell 時傳遞的參數將部署腳本(例如serverB.sh/serverC.sh )從服務器 A 復制到相應的服務器 serverB 或 serverC,例如:
/root/Pictures/JenkinsTesting/JenkinsBuildMain.sh B
在詹金斯的“構建”部分下的詹金斯“執行外殼”中,其中 B 是要部署的服務器名稱。 即,如果我在上述語句中再次將 C 作為參數傳遞,那么主腳本會將 serverC.sh 從服務器 A 復制到服務器 C 並將戰爭部署到該服務器注意:-所有部署步驟都寫在 serverC.sh 中,用於服務器 C和 serverB.sh 用於服務器 B。
同樣,我的部署在 jenkins 中分為兩步, Job1 和 Job2。
1.通過將源作為git存儲庫創建war文件,使用SSH訪問(比如服務器D)2.成功執行Job1后,它將觸發Job2。
在 Job2 中,我正在Build部分下執行 JenkinsBuildMain.sh 文件。
問題是如果我在 jenkins 的 .ssh 目錄中添加 id_rsa 和 id_rsa.pub 文件以訪問 git 服務器(服務器 D),同時我無法通過 SSH 連接訪問服務器 B 等其他服務器進行部署。 同樣,如果我在 .ssh 中為部署服務器(服務器 B)添加 id_rsa 和 id_rsa.pub 文件,則 Git 服務器無法同時訪問。 這意味着一次只能使用 SSH 連接訪問一台服務器。
有沒有辦法使用相同的密鑰或不同的密鑰訪問兩個服務器?
或者我們可以為 ssh 連接添加多個密鑰,以便它可以訪問服務器,服務器 D用於 git clone 和服務器 B 用於將腳本文件從 jenkins 服務器復制到服務器 B 。
當我這樣做時,我收到以下消息: “主機密鑰驗證失敗。失去連接”
目前我可以一次連接一台服務器。 如果它連接到 Git 服務器,則 shell 腳本文件無法復制,如上述錯誤,如果我能夠設置復制文件,則 git 服務器無法訪問說
返回狀態碼 128。權限被拒絕 (PublicKey,gssapi-keyex,gssapi-mic,password)....
有沒有辦法使用 Jenkins 為不同的服務器添加多個 ssh 連接密鑰?
Bellow 是我的腳本代碼,用於將部署腳本復制到遠程服務器。
JenkinsMainScript.sh:
if [ $serverId = 'B' ]
then
scp $scriptSourcePath/serverB.sh $serverBPathToCopyTheFile/
ssh -l $serverBUserName $IpAddr $serverBPathToExecuteTheFile/serverB.sh
else
echo " No Matching condition found..."
fi
這是 Jenkins 中為 job2 配置的屏幕截圖:
誰可以幫我這個事。 提前致謝
而不是直接執行scp $scriptSourcePath/serverB.sh $serverBPathToCopyTheFile/
,它使用默認的~/.ssh/id_rsa
私鑰文件,您可以將此private key
作為參數傳遞。
scp
和ssh
都接受私鑰文件作為參數。 為此目的使用參數-i <file path>
。 您可以執行man ssh
以了解所有命令行選項。 希望這對你有意義。
ssh -i <private key file path> ...
scp -i <private key file path> ...
man ssh
顯示選項i
的以下結果。
-i identity_file
Selects a file from which the identity (private key) for public key authenti-
cation is read. The default is ~/.ssh/id_dsa, ~/.ssh/id_ecdsa,
~/.ssh/id_ed25519 and ~/.ssh/id_rsa. Identity files may also be specified on
a per-host basis in the configuration file. It is possible to have multiple
-i options (and multiple identities specified in configuration files). If no
certificates have been explicitly specified by the CertificateFile directive,
ssh will also try to load certificate information from the filename obtained
by appending -cert.pub to identity filenames.
man scp
顯示選項i
的以下結果。
-i identity_file
Selects the file from which the identity (private key) for public key authen-
tication is read. This option is directly passed to ssh(1).
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.