@@ -43,3 +43,351 @@ SSH 代理設置:
4343* ` proxy_fingerprint ` - 代理主機公鑰的 SHA256 指紋,預設為跳過驗證
4444* ` proxy_use_insecure_cipher ` - 使用不安全的加密方式,請參閱 [ #56 ] ( https://github.com/appleboy/ssh-action/issues/56 )
4545* ` proxy_cipher ` - 允許的加密算法。如果未指定,則使用合理的算法
46+
47+ ## 使用方式
48+
49+ 執行遠端 SSH 命令
50+
51+ ``` yaml
52+ name : remote ssh command
53+ on : [push]
54+ jobs :
55+
56+ build :
57+ name : Build
58+ runs-on : ubuntu-latest
59+ steps :
60+ - name : executing remote ssh commands using password
61+ uses : appleboy/ssh-action@v0.1.8
62+ with :
63+ host : ${{ secrets.HOST }}
64+ username : ${{ secrets.USERNAME }}
65+ password : ${{ secrets.PASSWORD }}
66+ port : ${{ secrets.PORT }}
67+ script : whoami
68+ ` ` `
69+
70+ 畫面輸出
71+
72+ ` ` ` sh
73+ ======CMD======
74+ whoami
75+ ======END======
76+ out : ***
77+ ==============================================
78+ ✅ Successfully executed commands to all host.
79+ ==============================================
80+ ```
81+
82+ ### 設置 SSH 金鑰
83+
84+ 請在創建 SSH 金鑰並使用 SSH 金鑰時遵循以下步驟。最佳做法是在本地機器上創建 SSH 金鑰而不是遠端機器上。請使用 Github Secrets 中指定的用戶名登錄。生成 RSA 金鑰:
85+
86+ ### 生成 RSA 金鑰
87+
88+ ``` bash
89+ ssh-keygen -t rsa -b 4096 -C " your_email@example.com"
90+ ```
91+
92+ ### 生成 ed25519 金鑰
93+
94+ ``` bash
95+ ssh-keygen -t ed25519 -a 200 -C " your_email@example.com"
96+ ```
97+
98+ 將新生成的金鑰添加到已授權的金鑰中。詳細了解已授權的金鑰請點擊[ 此處] ( https://www.ssh.com/ssh/authorized_keys/ ) .
99+
100+ ### 將 RSA 金鑰添加到已授權金鑰中
101+
102+ ``` bash
103+ cat .ssh/id_rsa.pub | ssh b@B ' cat >> .ssh/authorized_keys'
104+ ```
105+
106+ ### 將 ed25519 金鑰添加到已授權金鑰中
107+
108+ ``` bash
109+ cat .ssh/id_ed25519.pub | ssh b@B ' cat >> .ssh/authorized_keys'
110+ ```
111+
112+ 複製私鑰內容,然後將其粘貼到 Github Secrets 中。
113+
114+ ### 複製 rsa 私鑰內容
115+
116+ ``` bash
117+ clip < ~ /.ssh/id_rsa
118+ ```
119+
120+ ### 複製 ed25519 私鑰內容
121+
122+ ``` bash
123+ clip < ~ /.ssh/id_ed25519
124+ ```
125+
126+ 有關無需密碼登錄 SSH 的詳細信息,請[ 參見該網站] ( http://www.linuxproblem.org/art_9.html ) 。
127+
128+ ** 來自讀者的注意事項** : 根據您的 SSH 版本,您可能還需要進行以下更改:
129+
130+ * 將公鑰放在 ` .ssh/authorized_keys2 ` 中
131+ * 將 ` .ssh ` 的權限更改為700
132+ * 將 ` .ssh/authorized_keys2 ` 的權限更改為640
133+
134+ ### 如果你使用的是 OpenSSH
135+
136+ 如果您正在使用 OpenSSH,並出現以下錯誤:
137+
138+ ``` bash
139+ ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey]
140+ ```
141+
142+ 請確保您所選擇的密鑰演算法得到支援。在 Ubuntu 20.04 或更高版本上,您必須明確允許使用 SSH-RSA 演算法。請在 OpenSSH 守護進程文件中添加以下行(它可以是 ` /etc/ssh/sshd_config ` 或 ` /etc/ssh/sshd_config.d/ ` 中的一個附著文件):
143+
144+ ``` bash
145+ CASignatureAlgorithms +ssh-rsa
146+ ```
147+
148+ 或者,` Ed25519 ` 密鑰在 OpenSSH 中默認被接受。如果需要,您可以使用它來替代 RSA。
149+
150+ ``` bash
151+ ssh-keygen -t ed25519 -a 200 -C " your_email@example.com"
152+ ```
153+
154+ ### Example
155+
156+ #### 使用密碼執行遠端 SSH 命令
157+
158+ ``` yaml
159+ - name : executing remote ssh commands using password
160+ uses : appleboy/ssh-action@v0.1.8
161+ with :
162+ host : ${{ secrets.HOST }}
163+ username : ${{ secrets.USERNAME }}
164+ password : ${{ secrets.PASSWORD }}
165+ port : ${{ secrets.PORT }}
166+ script : whoami
167+ ` ` `
168+
169+ #### 使用私鑰
170+
171+ ` ` ` yaml
172+ - name : executing remote ssh commands using ssh key
173+ uses : appleboy/ssh-action@v0.1.8
174+ with :
175+ host : ${{ secrets.HOST }}
176+ username : ${{ secrets.USERNAME }}
177+ key : ${{ secrets.KEY }}
178+ port : ${{ secrets.PORT }}
179+ script : whoami
180+ ` ` `
181+
182+ #### 多個命令
183+
184+ ` ` ` yaml
185+ - name : multiple command
186+ uses : appleboy/ssh-action@v0.1.8
187+ with :
188+ host : ${{ secrets.HOST }}
189+ username : ${{ secrets.USERNAME }}
190+ key : ${{ secrets.KEY }}
191+ port : ${{ secrets.PORT }}
192+ script : |
193+ whoami
194+ ls -al
195+ ` ` `
196+
197+ 
198+
199+ #### 多台主機
200+
201+ ` ` ` diff
202+ - name : multiple host
203+ uses : appleboy/ssh-action@v0.1.8
204+ with :
205+ - host : " foo.com"
206+ + host : " foo.com,bar.com"
207+ username : ${{ secrets.USERNAME }}
208+ key : ${{ secrets.KEY }}
209+ port : ${{ secrets.PORT }}
210+ script : |
211+ whoami
212+ ls -al
213+ ` ` `
214+
215+ #### 多個不同端口的主機
216+
217+ ` ` ` diff
218+ - name : multiple host
219+ uses : appleboy/ssh-action@v0.1.8
220+ with :
221+ - host : " foo.com"
222+ + host : " foo.com:1234,bar.com:5678"
223+ username : ${{ secrets.USERNAME }}
224+ key : ${{ secrets.KEY }}
225+ script : |
226+ whoami
227+ ls -al
228+ ` ` `
229+
230+ #### 在多個主機上同步執行
231+
232+ ` ` ` diff
233+ - name : multiple host
234+ uses : appleboy/ssh-action@v0.1.8
235+ with :
236+ host : " foo.com,bar.com"
237+ + sync : true
238+ username : ${{ secrets.USERNAME }}
239+ key : ${{ secrets.KEY }}
240+ port : ${{ secrets.PORT }}
241+ script : |
242+ whoami
243+ ls -al
244+ ` ` `
245+
246+ #### 將環境變量傳遞到 Shell 腳本
247+
248+ ` ` ` diff
249+ - name : pass environment
250+ uses : appleboy/ssh-action@v0.1.8
251+ + env :
252+ + FOO : " BAR"
253+ + BAR : " FOO"
254+ + SHA : ${{ github.sha }}
255+ with :
256+ host : ${{ secrets.HOST }}
257+ username : ${{ secrets.USERNAME }}
258+ key : ${{ secrets.KEY }}
259+ port : ${{ secrets.PORT }}
260+ + envs : FOO,BAR,SHA
261+ script : |
262+ echo "I am $FOO"
263+ echo "I am $BAR"
264+ echo "sha: $SHA"
265+ ` ` `
266+
267+ _在 ` env` 對象中,您需要將每個環境變量作為字符串傳遞,傳遞 `Integer` 數據類型或任何其他類型可能會產生意外結果。_
268+
269+ # ### 在第一次失敗後停止腳本
270+
271+ > ex: missing `abc` folder
272+
273+ ` ` ` diff
274+ - name: stop script if command error
275+ uses: appleboy/ssh-action@v0.1.8
276+ with:
277+ host: ${{ secrets.HOST }}
278+ username: ${{ secrets.USERNAME }}
279+ key: ${{ secrets.KEY }}
280+ port: ${{ secrets.PORT }}
281+ + script_stop: true
282+ script: |
283+ mkdir abc/def
284+ ls -al
285+ ` ` `
286+
287+ 畫面輸出 :
288+
289+ ` ` ` sh
290+ ======CMD======
291+ mkdir abc/def
292+ ls -al
293+
294+ ======END======
295+ 2019/11/21 01:16:21 Process exited with status 1
296+ err: mkdir: cannot create directory ‘abc/def’: No such file or directory
297+ ##[error]Docker run failed with exit code 1
298+ ` ` `
299+
300+ # ### 如何使用 `ProxyCommand` 連接遠程服務器?
301+
302+ ` ` ` bash
303+ +--------+ +----------+ +-----------+
304+ | Laptop | <--> | Jumphost | <--> | FooServer |
305+ +--------+ +----------+ +-----------+
306+ ` ` `
307+
308+ 在您的 `~/.ssh/config` 文件中,您會看到以下內容。
309+
310+ ` ` ` bash
311+ Host Jumphost
312+ HostName Jumphost
313+ User ubuntu
314+ Port 22
315+ IdentityFile ~/.ssh/keys/jump_host.pem
316+
317+ Host FooServer
318+ HostName FooServer
319+ User ubuntu
320+ Port 22
321+ ProxyCommand ssh -q -W %h:%p Jumphost
322+ ` ` `
323+
324+ # ### 如何將其轉換為 GitHubActions 的 YAML 格式?
325+
326+ ` ` ` diff
327+ - name: ssh proxy command
328+ uses: appleboy/ssh-action@v0.1.8
329+ with:
330+ host: ${{ secrets.HOST }}
331+ username: ${{ secrets.USERNAME }}
332+ key: ${{ secrets.KEY }}
333+ port: ${{ secrets.PORT }}
334+ + proxy_host: ${{ secrets.PROXY_HOST }}
335+ + proxy_username: ${{ secrets.PROXY_USERNAME }}
336+ + proxy_key: ${{ secrets.PROXY_KEY }}
337+ + proxy_port: ${{ secrets.PROXY_PORT }}
338+ script: |
339+ mkdir abc/def
340+ ls -al
341+ ` ` `
342+
343+ # ### 如何保護私鑰?
344+
345+ 密碼短語通常用於加密私鑰。這使得攻擊者無法單獨使用密鑰文件。文件泄露可能來自備份或停用的硬件,黑客通常可以從受攻擊系統中洩露文件。因此,保護私鑰非常重要。
346+
347+ ` ` ` diff
348+ - name: ssh key passphrase
349+ uses: appleboy/ssh-action@v0.1.8
350+ with:
351+ host: ${{ secrets.HOST }}
352+ username: ${{ secrets.USERNAME }}
353+ key: ${{ secrets.KEY }}
354+ port: ${{ secrets.PORT }}
355+ + passphrase: ${{ secrets.PASSPHRASE }}
356+ script: |
357+ whoami
358+ ls -al
359+ ` ` `
360+
361+ # ### 使用主機指紋驗證
362+
363+ 設置 SSH 主機指紋驗證可以幫助防止中間人攻擊。在設置之前,運行以下命令以獲取 SSH 主機指紋。請記得將 `ed25519` 替換為您的適當金鑰類型(`rsa`、 `dsa`等),而 `example.com` 則替換為您的主機。
364+
365+ 現代 OpenSSH 版本中,需要提取的_默認金鑰_類型是 `rsa`(從版本 5.1 開始)、`ecdsa`(從版本 6.0 開始)和 `ed25519`(從版本 6.7 開始)。
366+
367+ ` ` ` sh
368+ ssh example.com ssh-keygen -l -f /etc/ssh/ssh_host_ed25519_key.pub | cut -d ' ' -f2
369+ ` ` `
370+
371+ 現在您可以調整您的配置:
372+
373+ ` ` ` diff
374+ - name: ssh key passphrase
375+ uses: appleboy/ssh-action@v0.1.8
376+ with:
377+ host: ${{ secrets.HOST }}
378+ username: ${{ secrets.USERNAME }}
379+ key: ${{ secrets.KEY }}
380+ port: ${{ secrets.PORT }}
381+ + fingerprint: ${{ secrets.FINGERPRINT }}
382+ script: |
383+ whoami
384+ ls -al
385+ ` ` `
386+
387+ # # 貢獻
388+
389+ 我們非常希望您為 `appleboy/ssh-action` 做出貢獻,歡迎提交請求!
390+
391+ # # 授權方式
392+
393+ 本項目中的腳本和文檔采用 [MIT](LICENSE) 許可證 發布。
0 commit comments