我想編寫一個腳本,該gpg
腳本將運行一個名為“file”且密碼為“test”的檔案。
通常,當我使用 時gpg
,我通常只是運行gpg -c file
,它會要求我輸入密碼。但由於我希望該腳本獨立完成所有操作,因此我想提供密碼作為命令的一部分。
現在,當我嘗試使用:時gpg -c file --passphrase test
,它輸出:
用法: gpg [選項] --對稱 [檔名]
它想要我使用哪個接縫gpg --passphrase test --symmetric file
。但如果我這樣做,它會彈出一個對話框,詢問我要使用的密碼;這不是我想要的。
我如何正確設定參數?
答案1
在 GnuPG 中,選項必須位於指令之前,因此--passphrase
選項必須位於 之前--symmetric
。
關於 pin 輸入窗口,無論如何都會彈出(儘管您使用--passphrase
),您可能已經在使用 GnuPG 2,它需要--batch
與 一起使用--passphrase
。從手冊頁:
--passphrase string
Use string as the passphrase. This can only be used if only one
passphrase is supplied. Obviously, this is of very questionable
security on a multi-user system. Don't use this option if you
can avoid it. Note that this passphrase is only used if the
option --batch has also been given. This is different from
GnuPG version 1.x.
請注意,在多用戶系統上,所有其他用戶都能夠讀取您的命令列,因此在執行 GnuPG 時也能夠讀取密碼。最好使用其他--passphrase-*
選項之一來從文件或管道中讀取。
答案2
使用--pinentry-mode loopback
與--passphrase
&配合使用--passphrase-[file/fd]
,可以讓您輸入新訊息,以防檔案名稱衝突,例如:
File 'xyz.gpg' exists. Overwrite? (y/N)n
Enter new filename: xyz2.gpg
不像--batch
那樣很快就會失敗,說...failed: File exists
如果您最初添加了詳細選項 ( -v
),您應該會看到類似以下內容:
$ gpg -v -c file --pinentry-mode loopback --passphrase-file=passfile
gpg: Note: '--pinentry-mode' is not considered an option
gpg: Note: '--passphrase-file=passfile' is not considered an option
usage: gpg [options] --symmetric [filename]
非常清楚地表明它不喜歡將-c
( --symmetric
) 放在第一位。
--passphrase
我認為 gpg2 忽略選項(除非伴隨)的行為--batch
是一個錯誤。
答案3
如果gpg --version
報告v2,則需要新增該--batch
選項。
根據語法輸出,您可能正在使用 v1,在這種情況下您需要:
gpg --passphrase PASS -c --no-use-agent FILE
請注意,選項的順序並不重要;但是,任何文件都必須是最後一個參數。